Submission #1060645

# Submission time Handle Problem Language Result Execution time Memory
1060645 2024-08-15T20:16:58 Z NemanjaSo2005 Pipes (CEOI15_pipes) C++17
80 / 100
5000 ms 65536 KB
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5;
int N,M,vel[maxn],rod[maxn],dub[maxn],lift[maxn][17],kol[maxn];
vector<int> stablo[maxn];
vector<pair<int,int>> R;
vector<int> V;
void dfs(int gde,int pret){
   V.push_back(gde);
   lift[gde][0]=pret;
   for(int x:stablo[gde])
      if(x!=pret)
         dfs(x,gde);
}
inline int getp(int x){
   if(rod[x]==x)
      return x;
   rod[x]=getp(rod[x]);
   return rod[x];
}
inline void spoj(int a,int b,bool ost=false){
   int oa=a,ob=b;
   a=getp(a);
   b=getp(b);
   if(vel[a]<vel[b]){
      swap(a,b);
      swap(oa,ob);
   }
   rod[b]=a;
   vel[a]+=vel[b];
   if(ost){
      stablo[oa].push_back(ob);
      stablo[ob].push_back(oa);
   }
}
int main(){
   int a,b;
   cin>>N>>M;
   for(int i=1;i<=N;i++){
      vel[i]=1;
      rod[i]=i;
   }
   while(M--){
      cin>>a>>b;
      if(getp(a)!=getp(b))
         spoj(a,b,true);
   }
   for(int i=1;i<=N;i++)
      if(lift[i][0]==0)
         dfs(i,0);
   dub[0]=-1;
   for(int x:V)
      dub[x]=dub[lift[x][0]]+1;
   for(int j=1;j<=16;j++)
      for(int i=1;i<=N;i++)
         lift[i][j]=lift[lift[i][j-1]][j-1];

   cin.clear(); // Clear the EOF flag
   cin.seekg(0, std::ios::beg);

   cin>>N>>M;
   for(int i=1;i<=N;i++){
      vel[i]=1;
      rod[i]=i;
   }
   while(M--){
      cin>>a>>b;
      if(getp(a)==getp(b)){
         kol[a]++;
         kol[b]++;

         if(dub[a]>dub[b])
            swap(a,b);
         if(dub[b]>dub[a]){
            for(int i=0;i<=16;i++)
               if((dub[b]-dub[a])&(1<<i))
                  b=lift[b][i];
         }
         if(a!=b){
            for(int i=16;i>=0;i--)
               if(lift[a][i]!=lift[b][i]){
                  a=lift[a][i];
                  b=lift[b][i];
               }
            a=lift[a][0];
         }
         kol[a]-=2;
      }
      else
         spoj(a,b,false);
   }
   int x;
   for(int i=V.size()-1;i>=0;i--){
      x=V[i];
      for(int y:stablo[x])
         if(y!=lift[x][0])
            kol[x]+=kol[y];
      if(kol[x]==0 and lift[x][0]!=0)
         R.push_back({min(x,lift[x][0]),max(x,lift[x][0])});
   }
   sort(R.begin(),R.end());
   for(auto x:R)
      cout<<x.first<<" "<<x.second<<"\n";
  // inf.close();
   // ouf.close();
   return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 2 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6748 KB Output is correct
2 Correct 13 ms 7160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 387 ms 6748 KB Output is correct
2 Correct 398 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 699 ms 7048 KB Output is correct
2 Correct 804 ms 7000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1156 ms 9540 KB Output is correct
2 Correct 1016 ms 9864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1492 ms 13528 KB Output is correct
2 Correct 1303 ms 13700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2450 ms 13904 KB Output is correct
2 Correct 2357 ms 14036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3287 ms 14732 KB Output is correct
2 Correct 3110 ms 14752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4103 ms 14744 KB Output is correct
2 Runtime error 4011 ms 65536 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5013 ms 14564 KB Time limit exceeded
2 Halted 0 ms 0 KB -