codeforces-755【C思维、并查集】
来源:互联网 发布:网络主播琪琪 编辑:程序博客网 时间:2024/06/05 10:50
题目链接:点击打开链接
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int n;bool shu[1000020]={1,1};void get(){for(int i=2;i<=1000010;i++){if(shu[i])continue;for(int j=i*2;j<=1000010;j+=i){shu[j]=1;}}}int main(){get();while(~scanf("%d",&n)){int i;for(i=1;i<=1000;i++){if(shu[n*i+1])break;}printf("%d\n",i);}return 0;}
题目链接:点击打开链接
#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<map>#include<iostream>using namespace std;int n,m;string str;map<string,int> M;int main(){while(~scanf("%d%d",&n,&m)){M.clear();for(int i=1;i<=n;i++){cin>>str;M[str]=1;}int cnt=0;for(int i=1;i<=m;i++){cin>>str;if(M[str])cnt++;}if(n>m)puts("YES");else if(n<m)puts("NO");else{if(cnt&1)puts("YES");elseputs("NO");}}return 0;}
题目链接:点击打开链接
PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirected acyclic graphs with k vertices andk - 1 edges, where k is some integer. Note that one vertex is a valid tree.
There is exactly one relative living in each vertex of each tree, they have unique ids from 1 to n. For each Ball i we know the id of its most distant relative living on the same tree. If there are several such vertices, we only know the value of the one with smallest id among those.
How many trees are there in the forest?
The first line contains single integer n (1 ≤ n ≤ 104) — the number of Balls living in the forest.
The second line contains a sequence p1, p2, ..., pn of length n, where (1 ≤ pi ≤ n) holds and pi denotes the most distant from Ball irelative living on the same tree. If there are several most distant relatives living on the same tree, pi is the id of one with the smallest id.
It's guaranteed that the sequence p corresponds to some valid forest.
Hacking: To hack someone, you should provide a correct forest as a test. The sequence p will be calculated according to the forest and given to the solution you try to hack as input. Use the following format:
In the first line, output the integer n (1 ≤ n ≤ 104) — the number of Balls and the integer m (0 ≤ m < n) — the total number of edges in the forest. Then m lines should follow. The i-th of them should contain two integers ai and bi and represent an edge between vertices in which relatives ai and bi live. For example, the first sample is written as follows:
5 31 23 44 5
You should output the number of trees in the forest where PolandBall lives.
From the technical side, this problem is interactive. However, it should not affect you (except hacking) since there is no interaction.
52 1 5 3 3
2
11
1
In the first sample testcase, possible forest is: 1-2 3-4-5.
There are 2 trees overall.
In the second sample testcase, the only possible graph is one vertex and no edges. Therefore, there is only one tree.
~~英语太渣,没读明白意思 orz
大意:有 n 个点,从 1 ~ n,先将每个点与其距离最远的点的标号给出,然后要我们求出这 n 个点有多少棵树形成的,比如第一个例子,1-2,3-4-5 有两棵树。
思路:
1、直接的,可以用并查集处理。
2、我们可以发现,与每个点距离最远的点一定是树的某个端点,题中已经规定,如果存在多个距离相同远的端点,那么取编号最小的端点。也就是说,只要某些点存在于同一棵树上,那么这棵树上的给出的最远的点只有两个。当然还有特例,就是一棵树上只有一个点
还有就是碰见了这个 Idleness limit exceeded on test1
在使用多个输出函数连续进行多次输出时,有可能发现输出错误。因为下一个数据再上一个数据还没输出完毕,还在输出缓冲区中时,下一个 printf 就把另一个数据加入输出缓冲区,结果冲掉了原来的数据,出现输出错误。 在 prinf();后加上 fflush(stdout); 强制马上输出,避免错误。当然也可以换种输出:cout 就不需要这个了
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n;bool vis[10010];int main(){while(~scanf("%d",&n)){memset(vis,0,sizeof(vis));int cnt=0;for(int i=1;i<=n;i++){int x;scanf("%d",&x);if(i==x)cnt++;elsevis[x]=1;}int ans=0;for(int i=1;i<=n;i++){if(vis[i])ans++;}printf("%d\n",ans/2+cnt);fflush(stdout); // 用 cout 输出就不需要这一句了 }return 0;}/* 并查集实现 #include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;int n;int a[10010];int fa[10010];int find(int x){if(x==fa[x])return x;return fa[x]=find(fa[x]);}int main(){while(~scanf("%d",&n)){for(int i=1;i<=n;i++){fa[i]=i;scanf("%d",&a[i]);}for(int i=1;i<=n;i++){int nx=find(i);int ny=find(a[i]);if(nx!=ny)fa[ny]=nx;}int ans=0;for(int i=1;i<=n;i++){if(fa[i]==i)ans++;}cout<<ans<<endl;}return 0;}*/
- Codeforces 755C 【并查集】【思维】
- codeforces-755【C思维、并查集】
- CodeForces 722C DestroyingArray 并查集+思维题
- Codeforces 828C String Reconstruction【思维+并查集】
- Codeforces 722C Destroying Array【逆序(离线)思维+并查集】
- codeforces-445【A思维】【B并查集】
- Codeforces 699D. Fix a Tree【并查集+思维】
- Codeforces 469D Two Sets【思维+并查集】
- 【Codeforces 755 C PolandBall and Forest】+ 并查集
- codeforces 755c 并查集+迷之题意....
- CodeForces 218C 并查集
- 【CodeForces】445C Civilization 并查集
- codeforces 650C (并查集)
- codeforces 722C (并查集)
- codeforces 731C 并查集+贪心
- Codeforces 731C 并查集
- CodeForces - 731C Socks (并查集)
- codeforces 731C (并查集 水)
- 详细探究Spark的shuffle实现
- webstorm支持ES6语法设置
- ZStack-CC2530-2.5.1a 串口使用笔记(一)
- 搭建三层架构
- 使用IDEA进行远程调试
- codeforces-755【C思维、并查集】
- wampserver2.5 apache2.4.9配置https 图文
- php简单的tocken验证
- 受到黑客攻击
- 关系型数据库与非关系型数据库....
- IoC之HelloWorld
- 指定长度进行剪切字符串
- android中Otto事件总线框架的使用
- pip安装selenium