当前位置: 代码迷 >> 综合 >> Codeforces Round #464 (Div. 2)A. Love Triangle
  详细解决方案

Codeforces Round #464 (Div. 2)A. Love Triangle

热度:93   发布时间:2023-11-01 07:29:45.0

注意是爱三角,所以只能有3步。

#include <bits/stdc++.h>
using namespace std;
int a[5005];
int ok=0;
int n;
void dfs(int x)
{int step=0;for(int i=x;a[i]!=0&&step<=n;i=a[i]){//cout<<i<<"->";step++;if(a[i]==x&&step==3){ok=1;// cout<<"ok!"<<endl;break;}else if(a[i]==x){return;}}
}
int main()
{cin>>n;memset(a,0,sizeof(a));for(int i=1;i<=n;i++)scanf("%d",&a[i]);for(int i=1;i<=n;i++){dfs(i);if(ok)break;}if(ok)cout<<"YES"<<endl;else cout<<"NO"<<endl;
}

附上后台的测试数据:

Test: # 1, time:  15 ms., memory:  2028 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is YES
Test: # 2, time:  15 ms., memory:  2028 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is NO
Test: # 3, time:  0 ms., memory:  2032 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is YES
Test: # 4, time:  15 ms., memory:  2032 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is NO
Test: # 5, time:  0 ms., memory:  2028 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is YES
Test: # 6, time:  46 ms., memory:  2028 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is NO
Test: # 7, time:  30 ms., memory:  2032 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is YES
Test: # 8, time:  15 ms., memory:  2032 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is NO
Test: # 9, time:  0 ms., memory:  2032 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is NO
Test: # 10, time:  15 ms., memory:  2024 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is YES
Test: # 11, time:  15 ms., memory:  2036 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is YES
Test: # 12, time:  15 ms., memory:  2028 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is YES
Test: # 13, time:  15 ms., memory:  2028 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is NO
Test: # 14, time:  15 ms., memory:  2040 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is YES
Test: # 15, time:  0 ms., memory:  2024 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is YES
Test: # 16, time:  0 ms., memory:  2028 KB, exit code:  0, checker exit code:  0, verdict:  OK
Checker Log
ok answer is YES

  相关解决方案