Submission #650526


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(i=0;i<n;++i)
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define mp make_pair
#define pb push_back
#define fi first
#define sc second

const int INF=100000000;
vector<int> G[100000];

int ans=0;
int dist[100000];

void dfs(int now,int depth,int parent){
  int i;
  rep(i,G[now].size()){
    if(G[now][i]==parent) continue;

    if(dist[G[now][i]]>dist[now]+1){
      dist[G[now][i]]=dist[now]+1;
      //printf("go %d\n",G[now][i]);
      dfs(G[now][i],depth+1,now);
    }
    else if(dist[G[now][i]]!=INF){//ループ発見
      //printf(" loop %d\n",depth+1-dist[G[now][i]]);
      ans=max(ans,depth+1-dist[G[now][i]]);
    }
  }
}


int main(int argc, char const *argv[]) {
  int i;

  int n;
  cin >>n;
  rep(i,n){
    int x,y;
    scanf(" %d %d",&x,&y);
    --x;
    --y;
    G[x].pb(y);
    G[y].pb(x);
  }

  fill(dist,dist+n,INF);
  dist[0]=0;
  dfs(0,0,-1);

  std::cout << ans << std::endl;
  return 0;
}

Submission Info

Submission Time
Task F - ループを探せ
User imulan
Language C++11 (GCC 4.8.1)
Score 100
Code Size 1098 Byte
Status AC
Exec Time 127 ms
Memory 13168 KB

Compile Error

./Main.cpp: In function ‘int main(int, const char**)’:
./Main.cpp:43:26: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf(" %d %d",&x,&y);
                          ^

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 39 ms 3380 KB
00-sample-01 AC 34 ms 3372 KB
10-cycle-00 AC 37 ms 3436 KB
10-cycle-01 AC 34 ms 3368 KB
10-cycle-02 AC 36 ms 3404 KB
10-cycle-03 AC 37 ms 3540 KB
10-cycle-04 AC 45 ms 4400 KB
10-cycle-05 AC 127 ms 13168 KB
20-star-00 AC 37 ms 3372 KB
20-star-01 AC 35 ms 3404 KB
20-star-02 AC 37 ms 3496 KB
20-star-03 AC 41 ms 3764 KB
20-star-04 AC 89 ms 7348 KB
30-binary-00 AC 37 ms 3408 KB
30-binary-01 AC 38 ms 3504 KB
30-binary-02 AC 44 ms 3756 KB
30-binary-03 AC 110 ms 6956 KB
40-random-00 AC 37 ms 3408 KB
40-random-01 AC 34 ms 3368 KB
40-random-02 AC 37 ms 3376 KB
40-random-05 AC 35 ms 3404 KB
40-random-06 AC 38 ms 3500 KB
40-random-07 AC 37 ms 3404 KB
40-random-10 AC 42 ms 3788 KB
40-random-11 AC 44 ms 3784 KB
40-random-12 AC 44 ms 3784 KB
40-random-15 AC 111 ms 6976 KB
40-random-16 AC 106 ms 6976 KB
40-random-17 AC 110 ms 7080 KB