Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环
来源:互联网 发布:win10改mac地址 编辑:程序博客网 时间:2024/05/29 03:33
题目链接:http://codeforces.com/contest/742/problem/C
As you have noticed, there are lovely girls in Arpa’s land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush.
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
42 3 1 4
3
44 4 4 4
-1
42 1 4 3
1
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
题解:
错误的做法:
本以为t最大不会超过n,所以就用p[i][j]记录,记录距离结点i,j个距离的是哪个顶点。然后再依次枚举j,找到合适的t。
后来发现:t可以大于n,所以此方法失败。
正确的做法:
t为所有环的最小公倍数。(当环长为奇数时,直接取环长;当环长为偶数时,取环长的一半,因为可以刚好走到正对面)
错误做法:
#include<bits/stdc++.h>using namespace std;typedef long long LL;const double eps = 1e-6;const int INF = 2e9;const LL LNF = 9e18;const int mod = 1e9+7;const int maxn = 100+10;int n, a[maxn], p[maxn][maxn];void dfs(int f, int u, int k){ if(k>n) return; p[f][k] = u; dfs(f,a[u], k+1);}int main(){ cin>>n; for(int i = 1; i<=n; i++) cin>>a[i]; for(int i = 1; i<=n; i++) dfs(i,a[i],1); int ans = -1; for(int t = 1; t<=n; t++) { int i; for(i = 1; i<=n; i++) { int v = p[i][t]; if(p[v][t]!=i) break; } if(i==n+1) { ans = t; break; } } cout<<ans<<endl;}
正确做法:
#include<bits/stdc++.h>using namespace std;typedef long long LL;const double eps = 1e-6;const int INF = 2e9;const LL LNF = 9e18;const int mod = 1e9+7;const int maxn = 100+10;int n, a[maxn],vis[maxn];int gcd(int a, int b) { return b==0?a:gcd(b,a%b); }int dfs(int f, int i, int k){ if(vis[i]) return (i==f)?k:-1; vis[i] = 1; return dfs(f, a[i], k+1);}int main(){ cin>>n; for(int i = 1; i<=n; i++) cin>>a[i]; int ans = 1; for(int i = 1; i<=n; i++) { if(vis[i]) continue; int x = dfs(i,i,0); if(x==-1) { ans = -1; break; } if(!(x&1)) x >>= 1; ans = (ans*x)/gcd(ans,x); } cout<<ans<<endl;}
- Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round #383 (Div. 2)-C. Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round #383 (Div. 2)C-Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan(dfs&lcm)
- Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan dfs+最小公倍数
- Codeforces Round #383 (Div. 2)B -Arpa's loud Owf and Mehrdad's evil plan
- codeforces #383 div 2 C. Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round #383 (Div. 2)C.Arpa's loud Owf and Mehrdad's evil plan【思维+LCM】
- Codeforces Round #383 (Div. 1) 741A Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round#383C. Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round #383 (Div. 1) Problem A. Arpa's loud Owf and Mehrdad's evil plan 解题报告
- Codeforces 742C C. Arpa's loud Owf and Mehrdad's evil plan
- 【codeforces 742C】Arpa's loud Owf and Mehrdad's evil plan
- CodeForces 742C Arpa's loud Owf and Mehrdad's evil plan
- codeforces 742c Arpa's loud Owf and Mehrdad's evil plan
- 560. Subarray Sum Equals K
- 微信第三方平台开发中遇到的问题总结---java
- JS练习题(2)
- maven scope含义的说明
- HDU 3605 Escape(最大流+合并点)
- Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环
- C语言指针
- RBTree的插入操作
- C++实现排序算法
- Scala元组
- easyui 中日期控件清空文本框的值之后,获取仍有值
- fiddle 相关
- python module学习之beautifulsoup
- 8