Problem p02238 (C++) — buggy submission, verdict: Time Limit Exceeded

/*#include<iostream>
#include<cstdio>
using namespace std;
int a[1000000],n;
int binarysearch(int key)
{
  int left = 0;
  int right = n;
  while (left < right)
  {
    int mid = (left+right)/2;
    if(a[mid] == key)
      return 1;
    if(a[mid]<key) left = mid+1;
    else if(key<a[mid]) right = mid;
  }
   return 0 ;
}
int main (){
  int i,q,k,sum = 0;
  scanf("%d",&n);
  for(int i =0;i<n;i++)
    scanf("%d",&a[i]);
  scanf("%d",&q);
  for(int i = 0;i<q;i++)
  {
    scanf("%d",&k);
    if(binarysearch(k))
    sum++;
  }
  printf("%d\\n",sum);
  return 0;
}
*/
/*#include<iostream>
#include<cstdio>
#include<cstring>
#define M 1046527
#define NIL (-1)
#define L 14
using namespace std;
char H[M][L];
int getchar(char ch)
{
  if(ch == 'A')return 1;
  else if(ch == 'C')return 2;
  else if(ch == 'G')return 3;
  else if(ch == 'T')return 4;
  else return 0;
}
long long getkey(char str[])
{
  long long sum = 0,p = 1,i;
  for(i=  0;i<strlen(str);i++)
  {
    sum+=p*(getchar(str[i]));
    p*=5;
  }
  return sum;
}
int h1(int key){
  return key%M;
  }
int h2(int key)
{
  return 1+(key%(M-1));
}
int find (char str[])
{
  long long key,i,h;
  key = getkey(str);
  for(i = 0; ;i++)
  {
    h = (h1(key)+i*h2(key))%M;
    if(strcmp(H[h],str) == 0)
       return 1;
    else if (strlen(H[h]) == 0)
     return 0;
  }
  return 0 ;
}
int insert(char str[])
{
  long long key,i,h;
  key = getkey(str);
  for(i = 0; ;i++)
  {
    h = (h1(key)+i*h2(key))%M;
    if(strcmp(H[h],str) == 0)return 1;
     else if(strlen(H[h]) == 0)
         {
           strcpy(H[h],str);
           return 0;
         }
  }
  return 0;
}
int main ()
{
  int i,n,h;
  char str[L],com[9];
  for(i = 0;i<M;i++)
      H[i][0] ='\\0';
scanf("%d",&n);
for(i = 0;i<n;i++)
{

  scanf("%s %s",com,str);
  if(com[0] == 'i'){
    insert(str);
  }else {
    if(find(str))
    {
      printf("yes\\n");
    }else {
      printf("no\\n");

    }
  }
}
  return 0 ;
}
*/
/*#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int a[1000000],n;
int main ()
{
  cin>>n;
  for(int i = 0 ;i<n;i++)
     cin>>a[i];
  int q,k,sum = 0;
  cin>>q;
  for(int i = 0;i<q;i++)
     {
       cin>>k;
       if(*lower_bound(a,a+n,k)  == k)  sum++;
     }
     cout<<sum<<endl;
   return 0;
}
*/
/*#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
using namespace std;
int main ()
{
  stack<int> S;
  int a,b,x;
  string s;
  while(cin>>s)
  {
    if(s[0] == '+')
      {
        a = S.top(),S.pop();
        b = S.top(),S.pop();
        S.push(a+b);
      }
    else if(s[0] == '-')
    {
      b = S.top(),S.pop();
      a = S.top(),S.pop();
      S.push(a-b);
    }
    else if(s[0] == '*')
    {
      a = S.top(),S.pop();
      b = S.top(),S.pop();
      S.push(a*b);
    }
    else {
      S.push(atoi(s.c_str()));
    }

  }
  cout<<S.top()<<endl;
  return  0 ;
}
*/
/*#include<iostream>
#include<cstdio>
using namespace std;
int a[50],n;
int solve(int i,int m)
{
  if(m == 0) return 1;
  if(i>=n) return 0;
  int res = solve(i+1,m) || solve(i+1,m-a[i]);
  return res;
  }
int main()
{
  int q,M,i;
  scanf("%d",&n);
  for( i = 0;i<n;i++)
     scanf("%d",&a[i]);
  scanf("%d",&q);
  for(i = 0;i<q;i++)
  {
      scanf("%d",&M);
      if(solve(0,M)) printf("yes\\n");
      else printf("no\\n");
  }
 return 0 ;
}
*/
/*#include<iostream>
#include<cstdio>
#define MAX 100000
using namespace std;
int n,k;
long long T[MAX];
int check(long long p)
{
  int i = 0 ;
  for(int j = 0;j<k;j++)
  {
    long long s = 0;
    while (s+T[i] <= p)
    {
      s += T[i];
      i++;
      if(i == n) return n ;
    }
  }
  return i ;
}
int solve()
{
  int left = 0;
  int right = 100000*10000;
  int mid;
  while(right-left>1)
  {
    int mid = (right+left)/2;
    int v = check(mid);
    if(v>=n) right = mid;
    else left = mid;
  }
  return right;
}
int main ()
{
  cin>>n>>k;
  for(int i = 0;i<n;i++)
     cin>>T[i];
  long long ans = solve();
  cout<<ans<<endl;
  return 0 ;
}
*/
/*#include<iostream>
#include<cstdio>
#define _USE_MATH_DEFINES
#include<cmath>
using namespace std;
struct point{
  double x,y;
};
void koch(int n,point a,point b)
{
  if(n == 0) return ;
  point s,t,u;
  double th = M_PI *60.0/180.0;
  s.x = (2.0*a.x+1.0*b.x)/3.0;
  s.y = (2.0*a.y+1.0*b.y)/3.0;
  t.x = (1.0*a.x+2.0*b.x)/3.0;
  t.y = (1.0*a.y+2.0*b.y)/3.0;
  u.x = (t.x-s.x)*cos(th) - (t.y-s.y)*sin(th)+s.x;
  u.y = (t.x-s.x)*sin(th) + (t.y-s.y)*cos(th)+s.y;
  koch(n-1,a,s);
  printf("%.8f %.8f\\n",s.x,s.y);
  koch(n-1,s,u);
  printf("%.8f %.8f\\n",u.x,u.y);
  koch(n-1,u,t);
  printf("%.8f %.8f\\n",t.x,t.y);
  koch(n-1,t,b);
}
int main ()
{
  point a,b;
  int n;
  scanf("%d",&n);
  a.x = 0;
  a.y = 0;
  b.x = 100;
  b.y = 0;
  printf("%.8f %.8f\\n",a.x,a.y);
  koch(n,a,b);
  printf("%.8f %.8f\\n",b.x,b.y);
  return 0 ;
}
*/
/*#include<iostream>
#include<cstdio>
#define MAX 100005
#define NIL -1
using namespace std;
struct Node{
  int parent,left,right;
};
Node T[MAX];
int n,D[MAX],H[MAX];
void setdepth(int u,int d)
{
  if(u == NIL) return ;
  D[u] = d;
  setdepth(T[u].left,d+1);
  setdepth(T[u].right,d+1);
}
int setheight(int u)
{
  int h1 = 0,h2 = 0;
  if(T[u].left != NIL)
    h1 = setheight(T[u].left)+1;
  if(T[u].right != NIL)
     h2 = setheight(T[u].right) +1;

return H[u] = max(h1,h2);
}
int getsibling(int u)
{
  if(T[u].parent == NIL) return NIL;
  if(T[T[u].parent].left != u&&T[T[u].parent].left !=NIL)
      return T[T[u].parent].left;
  if(T[T[u].parent].right != u&&T[T[u].parent].right !=NIL)
      return T[T[u].parent].right;
  return NIL;
}
void print(int u)
{
  printf("node %d: ", u);
  printf("parent = %d, ",T[u].parent);
  printf("sibling = %d, ",getsibling(u));
  int deg = 0;
  if(T[u].left != NIL) deg++;
  if(T[u].right != NIL) deg++;
  printf("degree = %d, ",deg);
  printf("depth = %d, ",D[u]);
  printf("height = %d, ",H[u]);
  if(T[u].parent == NIL )
     printf("root\\n");
   else if(T[u].left == NIL&&T[u].right == NIL)
      printf("leaf\\n");
   else printf("internal node\\n");
}
int main (){
  int v,l,r,root = 0;
  scanf("%d",&n);
  for(int i = 0;i<n;i++)
    T[i].parent = NIL;
  for(int i = 0;i<n;i++)
  {
    scanf("%d %d %d",&v,&l,&r);
    T[v].left = 1;
    T[v].right = r;
    if(l != NIL )T[l].parent = v;
    if(r != NIL)T[r].parent = v;
  }
  for(int i = 0;i<n;i++)
    if(T[i].parent == NIL) root = 1;
  setdepth(root,0);
  setheight(root);
  for(int i = 0;i<n;i++)
    print(i);
  return 0 ;
}
#include<cstdio>
#include<iostream>
#define MAX 100000
using namespace std;
int parent(int i)
{
  return i/2;
}
int left(int i )
{
  return 2*i;
}
int right (int i)
{
  return 2*i+1;
}
int main ()
{
  int H,i,a[MAX+1];
  cin>>H;
  for( i = 1;i<=H;i++) cin>>a[i];
  for( i = 1;i<=H;i++)
    {
    cout<<"node "<<i<<": key = "<<a[i]<<", ";
    if(parent(i)>=1)cout<<"parent key = "<<a[parent(i)]<<", ";
    if(left(i)<=H)  cout<<"left key = "<<a[left(i)]<<", ";
    if(right(i)<=H) cout<<"right key = "<<a[right(i)]<<", ";
    cout<<endl;
    }
  return  0 ;
  }
*/
/*#include<iostream>
#include<cstdio>
#define MAX 2000000
using namespace std;
int H,a[MAX+1];
void max(int i)
{
  int l,r,large;
  l = 2*i;
  r = 2*i+1;
  if(l<=H&&a[l]>a[i])
    large = l;
  else large = i ;
  if(r<=H&&a[r]>a[large])
     large = r;
  if(large != i)
{  swap(a[i],a[large]);
  max(large);
  }
}
int main ()
{
   cin>>H;
   for(int i = 1;i<=H;i++) cin>>a[i];
   for(int i = H/2;i>=1;i--)
      max(i);
   for(int i = 1;i<=H;i++)
     cout<<" "<<a[i];
  cout<<endl;
  return 0 ;
  }
*/
/*#include<iostream>
#include<cstdio>
using namespace std;
int main ()
{
  long long int a[45];
  a[0]= 1;
  a[1] = 1;
  for(int i = 2;i<=44;i++)
     a[i] = a[i-1]+a[i-2];
  int n;
  scanf("%d",&n);
  printf("%lld\\n",a[n]);
  return 0 ;
}
*/
/*#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
static const int N = 1000;
int lcs(string X,string Y)
{
  int c[N+1][N+1];
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' '+X;
  Y = ' '+Y;
  for(int i = 1;i<=m;i++) c[i][0] = 0;
  for(int j = 1;j<=n;j++) c[0][j] = 0;
  for(int i = 1;i<=m;i++){
    for(int j = 1;j<=n;j++)
      {
        if(X[i] == Y[j])
         {
           c[i][j] = c[i-1][j-1]+1;
         }
        else {
          c[i][j] = max(c[i-1][j],c[i][j-1]);
        }
        maxl = max(maxl,c[i][j]);
      }
    }
  return maxl;
}
int main ()
{
  string s1,s2;
  int n;cin >> n;
  for(int i =0;i<n;i++){
    cin>>s1>>s2;
    cout<<lcs(s1,s2)<<endl;
  }
  return 0 ;
}
*/
/*#include<iostream>
#include<cstdio>
using namespace std;
static const int MAX  = 101;
int main ()
{
  int n,u,k,v;
  cin>>n;
  int M[MAX][MAX];
  for(int i  = 1;i<=n;i++)
    {
      for(int j = 1;j<=n;j++)
        {
          M[i][j] = 0;
        }
    }
  for(int i = 1;i<=n;i++)
   {
     cin>>u>>k;
     for(int j = 1;j<=k;j++)
       {
         cin>>v;
         M[u][v] = 1;
       }
   }
   for(int i = 1;i<=n;i++)
     {
       for(int j = 1;j<=n;j++)
        { if(j-1) cout<<" ";
          cout<<M[i][j];
     }
     cout<<endl;
   }
  return 0 ;
}
图*/
/*
#include<stdio.h>
#define N 100
#define  GRAY 1
#define BLACK 2
#define WHITE 0
int n,M[N][N];
int color[N],d[N],f[N],tt;
void dfs_visit(int u)
{
  int v;
  color[u] = GRAY;
  d[u] = ++tt;
  for(v = 0;v<n;v++)
  {
    if(M[u][v] == 0)
     continue;
  if(color[v] == 0)
    {
      dfs_visit(v);
    }
}
  color[u] = BLACK;
  f[u] == ++tt;
}
void dfs()
{
  int u;
  for(u = 0;u<n;u++)
     color[u] = 0;
  tt = 0;
  for(u = 0;u<n;u++)
  {
    if(color[u] == 0) dfs_visit(u);
  }
  for(u = 0;u<n;u++)
  {
    printf("%d %d %d\\n",u+1,d[u],f[u]);
  }
}
int main ()
{
  int u,v,k,i,j;
  scanf("%d",&n);
  for( i = 0;i<n;i++)
  {  for( j = 0;j<n;j++)
        M[i][j] = 0; }
  for( i =0;i<n;i++)
   {
     scanf("%d %d",&u,&k);
     u--;
     for( j = 0;j<k;j++)
       {
         scanf("%d",&v);
         v--;
         M[u][v] = 1;
       }
   }
   dfs();
  return  0 ;
}
*/

#include <cstdio>
#include <iostream>
using namespace std;
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define N 100
int M[N][N], n;
int color[N], d[N], f[N], tt;
void dfs_visit(int u) {
  int v;
  color[u] = GRAY;
  d[u] = ++tt;
  for (v = 0; v < n; v++) {
    if (M[u][v] == 0)
      continue;
    if (color[v] == WHITE) {
      dfs_visit(v);
    }
  }
  color[u] = BLACK;
  f[u] = ++tt;
}
void dfs() {
  int u;
  for (u = 0; u < n; u++)
    color[u] = WHITE;
  tt = 0;
  for (u = 0; u < n; u++) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }
  for (u = 0; u < n; u++) {
    printf("%d %d %d\\n", u + 1, d[u], f[u]);
  }
}
int main() {
  int u, v, k;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      M[i][j] = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &u, &k);
    u--;
    for (int j = 0; j < k; j++) {
      scanf("%d", &v);
      v--;
      M[u][v] = 1;
    }
  }
  dfs();
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define N 100
int M[N][N], n;
int color[N], d[N], f[N], tt;
void dfs_visit(int u) {
  int v;
  color[u] = GRAY;
  d[u] = ++tt;
  for (v = 0; v < n; v++) {
    if (M[u][v] == 0)
      continue;
    if (color[v] == WHITE) {
      dfs_visit(v);
    }
  }
  color[u] = BLACK;
  f[u] = ++tt;
}
void dfs() {
  int u;
  for (u = 0; u < n; u++)
    color[u] = WHITE;
  tt = 0;
  for (u = 0; u < n; u++) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }
  for (u = 0; u < n; u++) {
    printf("%d %d %d\\n", u + 1, d[u], f[u]);
  }
}
int main() {
  int u, v, k, i, j;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++)
      M[i][j] = 0;
  }
  for (i = 0; i < n; i++) {
    scanf("%d %d", &u, &k);
    u--;
    for (i = 0; j < k; j++) {
      scanf("%d", &v);
      v--;
      M[u][v] = 1;
    }
  }
  dfs();
  return 0;
}