Well, how do you feel about mobile phone? Your answer would probably be something like that "It's so convenient and benefits people a lot". However, If you ask Merlin this question on the New Year's Eve, he will definitely answer "What a trouble! I have to keep my fingers moving on the phone the whole night, because I have so many greeting message to send!" Yes, Merlin has such a long name list of his friends, and he would like to send a greeting message to each of them. What's worse, Merlin has another long name list of senders that have sent message to him, and he doesn't want to send another message to bother them Merlin is so polite that he always replies each message he receives immediately). So, before he begins to send message, he needs to figure to how many friends are left to be sent. Please write a program to help him. Here is something that you should note. First, Merlin's friend list is not ordered, and each name is alphabetic strings and case insensitive. These names are guaranteed to be not duplicated. Second, some senders may send more than one message to Merlin, therefore the sender list may be duplicated. Third, Merlin is known by so many people, that's why some message senders are even not included in his friend list.
There are multiple test cases. In each case, at the first line there are two numbers n and m (1<=n,m<=20000), which is the number of friends and the number of messages he has received. And then there are n lines of alphabetic strings(the length of each will be less than 10), indicating the names of Merlin's friends, one per line. After that there are m lines of alphabetic strings, which are the names of message senders. The input is terminated by n=0.
For each case, print one integer in one line which indicates the number of left friends he must send.
连续输入多组数据,每组数据的第一行是三个整数mu, nu, tu(tu<=50),分别表示稀疏矩阵的行数、列数和矩阵中非零元素的个数,数据之间用空格间隔,随后tu行输入稀疏矩阵的非零元素所在的行、列值和非零元素的值,每组数据的最后一行输入要查询的数据k。
19
01 |
#include <bits/stdc++.h> |
02 |
#define inf 0x3f3f3f3f |
06 |
int vis[1010],dis[1010]; |
14 |
memset (vis,0, sizeof (vis)); |
27 |
if (dis[j]<mm&&!vis[j]) |
42 |
if (!vis[j] && mmp[pos][j]<dis[j] ) |
43 |
dis[j] = mmp[pos][j] ; |
60 |
i==j?mmp[i][j]=0:mmp[i][j]=inf; |
66 |
mmp[a][b]=mmp[b][a]=c; |
n a^o7 !
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
All brave and intelligent fighters, next you will step into a distinctive battleground which is full of sweet and happiness. If you want to win the battle, you must do warm-up according to my instructions, which can make you in the best state preparing to fight. Now please relax yourself and enjoy the good moment. Before you raise your sharp sword to the enemy who guards the battleground, please allow me to tell you a true and romantic story about a samurai like you.
Samurai hh fell in love with girl ss, who is charming and demure. He realized the truth that he must spend his remaining life with ss, and resolved to pursue the hard-won affection. One day hh wrote a letter to ss, when she opens the letter with excitement her mind was in tangle. She found herself completely not to figure out the meaning about the letter, which said that "n 55!w ! pue n a^o7 ! n paau !". ss also immersed herself in guessing the meaning of that letter for a long time because of her adore to hh. Finally she called hh to ask the meaning of the letter. On the other side of the phone, hh was too nervous to say. Gradually he calmed down himself and told ss to reverse the letter and read it. Then on both ends of the phone comes the voice at the same time "i need u i love u and i miss u".
ss wants to tell each of you however you are Brave And Skilled, you shouldn't forget to express your loyal love and romantic feelings to your prince or princess.
Now the horn sounds for battle,do it by the following input and output. I think each of you will get an "Accepted" in this battle with pleasant mood.
Input
Input contains an integer T in the first line, and then T lines follow .Each line contains a message (only contain 'n5!wpuea^o7!' and
' '(space)), the message's length is no more than 100.
Output
Output the case number and the message. (As shown in the sample output)
Example Input
2n 55!w ! pue n a^o7 ! n paau !n5!wpuea^o7
Example Output
Case 1: i need u i love u and i miss uCase 2: loveandmisu
001 |
#include<bits/stdc++.h> |
003 |
char a[20]= "n5!wpuea^o7! " ; |
004 |
char b[20]= "usimdnaevoli " ; |
008 |
for ( int i = 0; i < 13; i++) |
012 |
for ( int i = 1; i <= T; i++) |
016 |
printf ( "Case %d: " , i); |
017 |
for ( int j = strlen (c)-1; j >= 0; j--) |
018 |
printf ( "%c" , t[c[j]]); |
无向图存在欧拉回路的充要条件
一个无向图存在欧拉回路,当且仅当该图所有顶点度数都为偶数,且该图是连通图。
有向图存在欧拉回路的充要条件
一个有向图存在欧拉回路,所有顶点的入度等于出度且该图是连通图。
混合图存在欧拉回路条件
要判断一个混合图G(V,E)(既有有向边又有无向边)是欧拉图,方法如下:
假设有一张图有向图G',在不论方向的情况下它与G同构。并且G'包含了G的所有有向边。那么如果存在一个图G'使得G'存在欧拉回路,那么G就存在欧拉回路。
其思路就将混合图转换成有向图判断。实现的时候,我们使用网络流的模型。现任意构造一个G'。用Ii表示第i个点的入度,Oi表示第i个点的出度。如果存在一个点k,|Ok-Ik|mod 2=1,那么G不存在欧拉回路。接下来则对于所有Ii>Oi的点从源点连到i一条容量为(Ii-Oi)/2的边,对于所有Ii<Oi的点从i连到汇点一条容量为(Oi-Ii)/2的边。如果对于节点U和V,无向边(U,V)∈E,那么U和V之间互相建立容量为无限大的边。如果此网络的最大流等于∑|Ii-Oi|/2,那么就存在欧拉回路。
数据结构实验之图论八:欧拉回路
Time Limit: 1000MS Memory limit: 65536K
题目描述
在哥尼斯堡的一个公园里,有七座桥将普雷格尔河中两个岛及岛与河岸连接起来。
能否走过这样的七座桥,并且每桥只走一次?瑞士数学家欧拉最终解决了这个问题并由此创立了拓扑学。欧拉通过对七桥问题的研究,不仅圆满地回答了哥尼斯堡七桥问题,并证明了更为广泛的有关一笔画的三条结论,人们通常称之为欧拉定理。对于一个连通图,通常把从某结点出发一笔画成所经过的路线叫做欧拉路。人们又通常把一笔画成回到出发点的欧拉路叫做欧拉回路。具有欧拉回路的图叫做欧拉图。
你的任务是:对于给定的一组无向图数据,判断其是否成其为欧拉图?
输入
连续T组数据输入,每组数据第一行给出两个正整数,分别表示结点数目N(1 < N <= 1000)和边数M;随后M行对应M条边,每行给出两个正整数,分别表示该边连通的两个结点的编号,结点从1~N编号。
输出
若为欧拉图输出1,否则输出0。
示例输入
1
6 10
1 2
2 3
3 1
4 5
5 6
6 4
1 4
1 6
3 4
3 6
示例输出
提示
如果无向图连通并且所有结点的度都是偶数,则存在欧拉回路,否则不存在。
来源
xam
示例程序
解法一:(并查集)
- #include<stdio.h>
- #include<string.h>
- int d[1010],f[1010];
- int t,n,m;
- int find(int x)
- {
- if(x!=f[x])
- f[x]=find(f[x]);
- return f[x];
- }
- void check(int x,int y)
- {
- int fx=find(x);
- int fy=find(y);
- if(fx!=fy)
- f[fx]=fy;
- }
- int solve()
- {
- int cnt=0;
- for(int i=1; i<=n; ++i)
- {
- if(f[i]==i)
- cnt++;
- }
- if(cnt!=1)
- return 0;
- for(int i=1;i<=n;++i)
- {
- if(d[i]%2==1)
- return 0;
- }
- return 1;
- }
- int main()
- {
- int t;
- scanf("%d",&t);
- while(t--)
- {
- scanf("%d%d",&n,&m);
- for(int i=1; i<=n; ++i)
- {
- f[i]=i;
- d[i]=0;
- }
- int u,v;
- for(int i=0; i<m; ++i)
- {
- scanf("%d%d",&u,&v);
- check(u,v);
- d[u]++;
- d[v]++;
- }
- if(solve())
- printf("1\n");
- else
- printf("0\n");
- }
- return 0;
- }
解法二:(DFS)
- #include<stdio.h>
- #include<string.h>
- int map[1010][1010],visited[10100],sum,d[2000],n;
- void DFS(int x)
- {
- int i;
- visited[x]=1;
- sum++;
- for(i=1;i<=n;i++)
- if(visited[i]==0&&map[x][i])
- DFS(i);
- }
- int main()
- {
- int i,j,m,k,t,l1,l2;
- scanf("%d",&t);
- while(t--)
- {
- sum=0;
- memset(map,0,sizeof(map));
- memset(visited,0,sizeof(visited));
- memset(d,0,sizeof(d));
- scanf("%d %d",&n,&m);
- for(i=0;i<m;i++)
- {
- scanf("%d %d",&l1,&l2);
- map[l1][l2]=1;
- map[l2][l1]=1;
- d[l1]++;
- d[l2]++;
- }
- DFS(l1);
- for(i=1;i<=n;i++)
- if(d[i]%2==1)
- break;
- if(i==n+1&&sum==n)
- printf("1\n");
- else
- printf("0\n");
- }
- }
解法三:(BFS)
- #include<stdio.h>
- #include<string.h>
- int map[1010][1010],visited[10100],sum,d[2000],d1[2000],n;
- void BFS(int s)
- {
- int out=0,in=0,v,i;
- visited[s]=1;
- sum++;
- d1[in++]=s;
- while(out<in)
- {
- v=d1[out++];
- for(i=1;i<=n;i++)
- if(visited[i]==0&&map[v][i])
- {
- visited[i]=1;
- sum++;
- d1[in++]=i;
- }
- }
- }
- int main()
- {
- int i,j,m,k,t,l1,l2;
- scanf("%d",&t);
- while(t--)
- {
- sum=0;
- memset(map,0,sizeof(map));
- memset(visited,0,sizeof(visited));
- memset(d,0,sizeof(d));
- scanf("%d %d",&n,&m);
- for(i=0;i<m;i++)
- {
- scanf("%d %d",&l1,&l2);
- map[l1][l2]=1;
- map[l2][l1]=1;
- d[l1]++;
- d[l2]++;
- }
- BFS(1);
- for(i=1;i<=n;i++)
- if(d[i]%2==1)
- break;
- if(i==n+1&&sum==n)
- printf("1\n");
- else
- printf("0\n");
- }
- }
图中点的度是指这个点相连的边的数目
一般来说,图可分为有向图和无向图。有向图的所有边都有方向,即确定了顶点到顶点的一个指向;而无向图的所有边都是双向的,即无向边所连接的两个顶点可以互相到达。在一些问题中,可以把无向图当作所有边都是正向和负向的两条有向边组成。顶点的度是指和该顶点相连的边的条数。特别是对于有向图来说,顶点的出边条数称为该顶点的出度,顶点的入边条数称为该项点的入度。
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
完美网络是连通网络的基础上要求去掉网络上任意一条线路,网络仍然是连通网络。求一个连通网络要至少增加多少条边可以成为完美网络。
Input
第一行输入一个数T代表测试数据个数(T<=20)。每个测试数据第一行2个数n,m 分别代表网络基站数和基站间线路数。基站的序号为从1到n。接下来m行两个数代表x,y 代表基站x,y间有一条线路。
(
0 < n < m < 10000)
Output
对于每个样例输出最少增加多少线路可以成为完美网络。每行输出一个结果。
Example Input
Example Output
2
1
01 |
#include<bits/stdc++.h> |
10 |
priority_queue< int , vector< int >, greater< int > >que; |
11 |
memset (dg,0, sizeof (dg)); |
Time Limit: 400MS
Memory Limit: 65536KB
Problem Description
随着卫星成像技术的应用,自然资源研究机构可以识别每一个棵树的种类。请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比。
Input
输入一组测试数据。数据的第1行给出一个正整数N (n <= 100000),N表示树的数量;随后N行,每行给出卫星观测到的一棵树的种类名称,树的名称是一个不超过20个字符的字符串,字符串由英文字母和空格组成,不区分大小写。
Output
按字典序输出各种树的种类名称和它占的百分比,中间以空格间隔,小数点后保留两位小数。
Example Input
2
This is an Appletree
this is an appletree
Example Output
this is an appletree 100.00%
01 |
#include <bits/stdc++.h> |
08 |
map<string, int >::iterator cnt; |
09 |
char s[10000],str[10000]; |
13 |
memset ( str, 0, sizeof (str) ); |
16 |
for (i=0; i< strlen (s); i++) |
17 |
if (s[i]>= 'A' &&s[i]<= 'Z' ) |
24 |
for (cnt=mmp.begin(); cnt!=mmp.end(); cnt++) |
26 |
x=(cnt->second*100.0)/(n*1.0); |
27 |
printf ( "%s %.2lf" ,cnt->first.c_str(),x); |
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
根据给定的一系列整数关键字和素数p,用除留余数法定义hash函数H(Key)=Key%p,将关键字映射到长度为p的哈希表中,用线性探测法解决冲突。重复关键字放在hash表中的同一位置。
Input
连续输入多组数据,每组输入数据第一行为两个正整数N(N <= 1500)和p(p >= N的最小素数),N是关键字总数,p是hash表长度,第2行给出N个正整数关键字,数字间以空格间隔。
Output
输出每个关键字在hash表中的位置,以空格间隔。注意最后一个数字后面不要有空格。
Example Input
5 5
21 21 21 21 21
4 5
24 15 61 88
4 5
24 39 61 15
5 5
24 39 61 15 39
Example Output
1 1 1 1 1
4 0 1 3
4 0 1 2
4 0 1 2 0
05 |
int insert( int *f, int k) |
09 |
while (f[j]!=0&&f[j]!=k) |
16 |
while ( scanf ( "%d %d" ,&n,&p)!=EOF) |
24 |
printf ( "%d%c" ,insert(hash,k),i==n-1? '\n' : ' ' ); |