Submission #1414868


Source Code Expand

#include<bits/stdc++.h>

using namespace std;


vector<int> e[2][100000];


int mem[2][100000];

int dfs(int p,int f,int pr){
int res=0;
 
  if(f==-1){
    int a=0,b=0;
    for(int i=0;i<e[0][p].size();i++)a=max(a,dfs(e[0][p][i] , 0,p) );
    for(int i=0;i<e[1][p].size();i++)b=max(b,dfs(e[1][p][i] , 1,p) );
    return max(a,b);
  }
  else {

    
    if(mem[f][p]!=-1)return mem[f][p];

    for(int i=0;i<e[f][p].size();i++)
      if(e[f][p][i]!=pr)
	res=max(res,dfs(e[f][p][i],f,p));

  }
  return mem[f][p]=res+1;

}

int main(){
  int n;
  int a,b,t;
  cin>>n;
  for(int i=0;i<n-1;i++){
    cin>>a>>b>>t;
    a--,b--;
    e[0][a].push_back(b);
    e[1][b].push_back(a);
    
    if(t==2) {
      e[0][b].push_back(a);
      e[1][a].push_back(b);
    }
    
  }
  memset(mem,-1,sizeof(mem));
  
  int ans=0;

  for(int i=0;i<n;i++){
    if(e[0][i].size()==1||e[1][i].size()==1)
      ans=max(ans,dfs(i,-1,-1));
  }

  cout<<ans-1<<endl;
  
  return 0;
}


Submission Info

Submission Time
Task D - Longest Path
User aizu_b
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1024 Byte
Status WA
Exec Time 149 ms
Memory 19712 KB

Judge Result

Set Name All
Score / Max Score 0 / 100
Status
AC × 16
WA × 16
Set Name Test Cases
All 00-sample-00.txt, 00-sample-01.txt, 10-path-00.txt, 10-path-01.txt, 10-path-02.txt, 10-path-03.txt, 10-path-04.txt, 20-path_special-00.txt, 20-path_special-01.txt, 20-path_special-02.txt, 30-star-00.txt, 30-star-01.txt, 30-star-02.txt, 30-star-03.txt, 40-binary-00.txt, 40-binary-01.txt, 40-binary-02.txt, 40-binary-03.txt, 40-binary-04.txt, 50-random-00.txt, 50-random-01.txt, 50-random-02.txt, 50-random-03.txt, 50-random-04.txt, 50-random-05.txt, 50-random-06.txt, 50-random-07.txt, 50-random-08.txt, 50-random-09.txt, 50-random-10.txt, 50-random-11.txt, 50-random-12.txt
Case Name Status Exec Time Memory
00-sample-00.txt AC 3 ms 5760 KB
00-sample-01.txt AC 3 ms 5760 KB
10-path-00.txt AC 3 ms 5760 KB
10-path-01.txt WA 134 ms 11520 KB
10-path-02.txt WA 32 ms 7168 KB
10-path-03.txt AC 42 ms 7680 KB
10-path-04.txt WA 88 ms 9600 KB
20-path_special-00.txt AC 137 ms 19200 KB
20-path_special-01.txt AC 149 ms 19712 KB
20-path_special-02.txt WA 121 ms 10368 KB
30-star-00.txt AC 114 ms 10992 KB
30-star-01.txt AC 6 ms 5888 KB
30-star-02.txt AC 48 ms 8056 KB
30-star-03.txt AC 50 ms 8056 KB
40-binary-00.txt AC 136 ms 11136 KB
40-binary-01.txt AC 119 ms 10624 KB
40-binary-02.txt AC 6 ms 5888 KB
40-binary-03.txt AC 101 ms 10240 KB
40-binary-04.txt AC 7 ms 5888 KB
50-random-00.txt WA 134 ms 11136 KB
50-random-01.txt WA 43 ms 7552 KB
50-random-02.txt WA 77 ms 9088 KB
50-random-03.txt WA 59 ms 8192 KB
50-random-04.txt WA 66 ms 8448 KB
50-random-05.txt WA 130 ms 10880 KB
50-random-06.txt WA 10 ms 6016 KB
50-random-07.txt WA 20 ms 6528 KB
50-random-08.txt WA 115 ms 10496 KB
50-random-09.txt WA 79 ms 9088 KB
50-random-10.txt WA 79 ms 8960 KB
50-random-11.txt AC 22 ms 6656 KB
50-random-12.txt WA 123 ms 10880 KB