CodeForces 760CPavel and barbecue
来源:互联网 发布:c语言编译器哪个好 编辑:程序博客网 时间:2024/04/28 12:20
http://codeforces.com/problemset/problem/760/C
题意看上去很复杂,整理出来就是给出一张有向图,并且起点和终点都是1-n里的数字且不重复,每次操作可以将一个数字换成另一个数字,求操作几次可以满足该图是完全图,因为起点的数字不会出现2次以上,终点也是如此,所以只要看这张图有几个环就可以了,并且这些环是不会相切的,环的个数是1则不用操作,如果大于1则多少个环就需要操作多少次。
#include<bits/stdc++.h>using namespace std;int ans;int ans1;int ma[222222];bool vis[222222];void dfs(int x){if(vis[ma[x]]==1){ans1++;}else {vis[ma[x]]=1;dfs(ma[x]);}}int main(){int n,m,k;while(scanf("%d",&n)!=EOF){ans=0;ans1=0; memset(ma,0,sizeof(ma)); memset(vis,0,sizeof(vis)); int _0=0; int i; int a; for(i=1;i<=n;i++) { cin>>a; ma[i]=a; } for(i=1;i<=n;i++) { cin>>a; if(a==1)_0++; } if(_0%2==0)ans++; for(i=1;i<=n;i++) { if(vis[i]==0) { vis[i]=1; dfs(i); } } if(ans1>1) ans+=ans1; else ans+=ans1-1; cout<<ans<<endl;}return 0;}
阅读全文
0 0
- CodeForces 760CPavel and barbecue
- 【codeforces 760C】Pavel and barbecue
- Codeforces 760C Pavel and barbecue【思维+Dfs】
- CodeForces 760C Pavel and barbecue【英文题】
- Codeforces Round #393 Pavel and barbecue
- C. Pavel and barbecue
- Pavel and barbecue
- Pavel and barbecue
- Codeforces Round #393 (Div. 2) C Pavel and barbecue(思维)
- Codeforces Round #393 (Div. 2)-C. Pavel and barbecue(并查集)
- cf 759 A Pavel and barbecue
- Barbecue 条形码
- A Barbecue
- CodeForces 760BFrodo and pillows
- 【codeforces 760A】Petr and a calendar
- 【codeforces 760B】Frodo and pillows
- Codeforces 760B Frodo and pillows 【二分】
- 【Codeforces 760 B Frodo and pillows】+ 二分
- 对贝叶斯理解以及解释贝叶斯函数
- Spring 自动装配
- 为什么Java的String设计为不可变的?
- HDU 3709 Balanced Number(数位DP)
- 反转二叉树(二叉树的镜像)
- CodeForces 760CPavel and barbecue
- 项目总结
- grub2配置原理分析
- 4.移动端的事件冒泡与捕获
- OA系统汽车/零部件行业解决方案
- 可见性与volatile关键字
- Google三驾马车:GFS、MapReduce和Bigtable
- 学习api的使用方式
- 数据分析--概率--002