Submission #1246350


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 114514
vector<int> G[MAX];
int de[MAX],pa[MAX];
int ans;
void dfs(int v,int p,int d){
  de[v]=d;
  pa[v]=p;
  for(int u:G[v]){
    if(u==p) continue;
    if(de[u]<0) dfs(u,v,d+1);
    else{
      //cout<<de[v]<<" "<<de[u]<<endl;
      ans=de[v]-de[u]+1;
      return;
    }
    if(~ans) return;
  }
  if(~ans) return;
}
signed main(){
  memset(de,-1,sizeof(de));
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    int x,y;
    cin>>x>>y;
    x--;y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  ans=-1;
  dfs(0,-1,0);
  cout<<ans<<endl;
  return 0;
}

Submission Info

Submission Time
Task F - ループを探せ
User beet
Language C++14 (GCC 5.4.1)
Score 100
Code Size 672 Byte
Status AC
Exec Time 80 ms
Memory 12416 KB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 29
Set Name Test Cases
All 00-sample-00, 00-sample-01, 10-cycle-00, 10-cycle-01, 10-cycle-02, 10-cycle-03, 10-cycle-04, 10-cycle-05, 20-star-00, 20-star-01, 20-star-02, 20-star-03, 20-star-04, 30-binary-00, 30-binary-01, 30-binary-02, 30-binary-03, 40-random-00, 40-random-01, 40-random-02, 40-random-05, 40-random-06, 40-random-07, 40-random-10, 40-random-11, 40-random-12, 40-random-15, 40-random-16, 40-random-17
Case Name Status Exec Time Memory
00-sample-00 AC 2 ms 3840 KB
00-sample-01 AC 2 ms 3840 KB
10-cycle-00 AC 2 ms 3840 KB
10-cycle-01 AC 2 ms 3840 KB
10-cycle-02 AC 2 ms 3840 KB
10-cycle-03 AC 3 ms 3968 KB
10-cycle-04 AC 9 ms 4736 KB
10-cycle-05 AC 80 ms 12416 KB
20-star-00 AC 2 ms 3840 KB
20-star-01 AC 2 ms 3840 KB
20-star-02 AC 3 ms 3840 KB
20-star-03 AC 8 ms 4352 KB
20-star-04 AC 66 ms 8564 KB
30-binary-00 AC 2 ms 3840 KB
30-binary-01 AC 3 ms 3840 KB
30-binary-02 AC 9 ms 4352 KB
30-binary-03 AC 77 ms 8960 KB
40-random-00 AC 3 ms 3840 KB
40-random-01 AC 2 ms 3840 KB
40-random-02 AC 2 ms 3840 KB
40-random-05 AC 3 ms 3840 KB
40-random-06 AC 3 ms 3840 KB
40-random-07 AC 3 ms 3840 KB
40-random-10 AC 9 ms 4224 KB
40-random-11 AC 9 ms 4224 KB
40-random-12 AC 9 ms 4224 KB
40-random-15 AC 73 ms 8320 KB
40-random-16 AC 73 ms 8320 KB
40-random-17 AC 77 ms 8320 KB