Codeforces Round #393 Pavel and barbecue
来源:互联网 发布:学生请假软件 编辑:程序博客网 时间:2024/04/30 11:13
每个串都要经过n个位置,每个位置都要有翻转和不翻转两种情况,根据 p [ i ] 求出组成多少个环,求连成一个环需要改变的次数(1个环ans = 0,否则ans = 环数)
每个串都要有翻转和不翻转两种情况,所以 b [ ]中1的个数应为奇数,否则ans+1
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>#include <vector>#include <map>#include <cmath>#include <stdlib.h>using namespace std;const double PI = acos(-1.0);const double eps = 1e-8;const int MAX = 2e5+10;const int mod = 1e9+7;int p[MAX], vis[MAX], b[MAX], n;void dfs(int i){ vis[i] = 1; if(!vis[p[i]])dfs(p[i]);}int main(){ cin>>n; int ans = 0; memset(vis, 0, sizeof(vis)); for(int i = 1; i<=n; ++i) scanf("%d", &p[i]); for(int i = 1; i<=n; ++i) if(!vis[i]) { dfs(i); ans++; } if(ans==1)ans--; int zero = 0, one = 0; for(int i = 1; i<=n; ++i) { scanf("%d", &b[i]); if(b[i])one++; else zero++; } if(one%2==0)ans++; cout<<ans<<endl; return 0;}
0 0
- Codeforces Round #393 Pavel and barbecue
- Codeforces Round #393 (Div. 2) C Pavel and barbecue(思维)
- Codeforces Round #393 (Div. 2)-C. Pavel and barbecue(并查集)
- 【codeforces 760C】Pavel and barbecue
- C. Pavel and barbecue
- Pavel and barbecue
- Pavel and barbecue
- Codeforces 760C Pavel and barbecue【思维+Dfs】
- CodeForces 760C Pavel and barbecue【英文题】
- cf 759 A Pavel and barbecue
- CodeForces 760CPavel and barbecue
- Codeforces Round #393 Frodo and pillows
- Codeforces Round #393 Petr and a calendar
- codeforces George and Round
- Codeforces Round #393 (Div. 2) Frodo and pillows
- Codeforces Round #393 (Div. 2) -B. Frodo and pillows(贪心)
- Codeforces Round #393 (Div. 2) B. Frodo and pillows
- Codeforces Round #216_div2_D.Valera and Fools
- 洛谷 P1345 [USACO5.4]奶牛的电信Telecowmunication
- 一次血的教训,阿里云rds mysql 数据库,本地化并日志恢复已删除的两天数据
- Volley之简单封装(1)
- LightOJ-1364 Expected Cards(期望dp)
- 使用OD从零开始Cracking
- Codeforces Round #393 Pavel and barbecue
- Mac使用Cocos2.2.6的最低配置
- 使用BOOL变量犯过的错误
- 【t084】数列
- 算法之路二:刘汝佳算法竞赛入门经典:c++结构体
- 克鲁斯卡尔算法:蓝桥杯 算法训练 安慰奶牛 (最小生成树)
- 流媒体协议—HLS
- 从任正菲“打港办”收编干儿子李一男的十大手法,看华为2017年春节前起诉6名前员工!
- 【颓废篇】easyx--2048