Submission #337315

#TimeUsernameProblemLanguageResultExecution timeMemory
337315GiselusRailway (BOI17_railway)C++14
100 / 100
177 ms24556 KiB
#include<cstdio> #include<algorithm> #include<vector> #define S 100007 using namespace std; vector < vector < int > > L; pair < int, int > E[S]; int num[S]; int R[S][20]; pair < int, int > t[S]; int dp[S]; bool ta[S]; int poz[S]; int pom = 0; void DFS(int x, int p){ poz[x] = poz[p]+1; pom++; num[x] = pom; for(int i = 1; i <= 19; i++){ R[x][i] = R[R[x][i-1]][i-1]; } for(int i = 0; i < L[x].size(); i++){ if(L[x][i] != p){ R[L[x][i]][0] = x; DFS(L[x][i],x); } } } void DFS2(int x, int p){ for(int i = 0 ; i < L[x].size();i++){ if(L[x][i] != p){ DFS2(L[x][i],x); dp[x] += dp[L[x][i]]; } } } int LCA(int x, int y){ if(poz[y] > poz[x]) swap(x,y); for(int i = 19;i >= 0;i--){ if(poz[R[x][i]] >= poz[y]) x = R[x][i]; } if(x == y) return x; for(int i = 19; i >= 0;i--){ if(R[x][i] != R[y][i]){ x = R[x][i]; y = R[y][i]; } } return R[x][0]; } int main(void){ int n,m,a,b,c,d,x,k,q; scanf("%d %d %d",&n,&m,&k); L.resize(n+7); for(int i = 1; i <= n-1; i++){ scanf("%d %d",&a,&b); E[i] = {a,b}; L[a].push_back(b); L[b].push_back(a); } R[1][0] = 1; DFS(1,0); while(m--){ scanf("%d",&q); for(int i = 0; i < q; i++){ scanf("%d",&x); t[i] = {num[x],x}; } sort(t,t+q); for(int i = 0 ; i < q;i++){ int lca = LCA(t[i].second,t[(i+1)%q].second); dp[t[i].second]++; dp[t[(i+1)%q].second]++; dp[lca]-=2; } } DFS2(1,0); int odp = 0; for(int i = 1; i <= n-1;i++){ if(num[E[i].first] > num[E[i].second]) swap(E[i].first,E[i].second); if(dp[E[i].second] >= 2*k){ odp++; ta[i] = 1; } } printf("%d\n",odp); for(int i = 1; i <= n-1;i++){ if(ta[i]) printf("%d ",i); } return 0; }

Compilation message (stderr)

railway.cpp: In function 'void DFS(int, int)':
railway.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for(int i = 0; i < L[x].size(); i++){
      |                    ~~^~~~~~~~~~~~~
railway.cpp: In function 'void DFS2(int, int)':
railway.cpp:33:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     for(int i = 0 ; i < L[x].size();i++){
      |                     ~~^~~~~~~~~~~~~
railway.cpp: In function 'int main()':
railway.cpp:61:17: warning: unused variable 'c' [-Wunused-variable]
   61 |     int n,m,a,b,c,d,x,k,q;
      |                 ^
railway.cpp:61:19: warning: unused variable 'd' [-Wunused-variable]
   61 |     int n,m,a,b,c,d,x,k,q;
      |                   ^
railway.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   62 |     scanf("%d %d %d",&n,&m,&k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
railway.cpp:65:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   65 |         scanf("%d %d",&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~~
railway.cpp:73:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   73 |         scanf("%d",&q);
      |         ~~~~~^~~~~~~~~
railway.cpp:75:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   75 |             scanf("%d",&x);
      |             ~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...