Submission #457696

#TimeUsernameProblemLanguageResultExecution timeMemory
457696vanicPipes (CEOI15_pipes)C++14
40 / 100
5095 ms21188 KiB
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <cassert> using namespace std; const int maxn=1e5+5; vector < pair < int, int > > ms[maxn]; struct union_find{ int parent[maxn]; union_find(){ for(int i=0; i<maxn; i++){ parent[i]=i; } } int find(int x){ if(parent[x]==x){ return x; } return parent[x]=find(parent[x]); } void update(int x, int y){ x=find(x); y=find(y); if(ms[x].size()>ms[y].size()){ swap(x, y); } parent[x]=y; pair < int, int > p; while(!ms[x].empty()){ p=ms[x].back(); ms[x].pop_back(); if(y==find(p.second)){ continue; } ms[y].push_back(p); } } bool query(int x, int y){ return find(x)==find(y); } }; struct union_find2{ int parent[maxn]; int sz[maxn]; union_find2(){ for(int i=0; i<maxn; i++){ parent[i]=i; sz[i]=1; } } int find(int x){ if(x==parent[x]){ return x; } return parent[x]=find(parent[x]); } void update(int x, int y){ x=find(x); y=find(y); if(sz[x]>sz[y]){ swap(x, y); } parent[x]=y; sz[y]+=sz[x]; } bool query(int x, int y){ return find(x)==find(y); } }; union_find U1; union_find2 U2; vector < int > put; bool bio[maxn]; bool dfs(int x, int prosl){ bool p=0; // cout << x << endl; if(bio[x]){ for(int i=0; i<(int)put.size(); i++){ if(put[i]==x){ p=1; } else if(p){ // cout << "upd " << put[i] << ' ' << put[i-1] << endl; U1.update(put[i], put[i-1]); } } return 1; } bio[x]=1; put.push_back(x); for(int i=0; i<(int)ms[x].size(); i++){ if(!p && U1.find(ms[x][i].second)==prosl){ p=1; } else if(U1.find(ms[x][i].second)!=x){ if(dfs(U1.find(ms[x][i].second), x)){ bio[x]=0; put.pop_back(); return 1; } } } put.pop_back(); bio[x]=0; return 0; } int main(){ int n, m; scanf("%d%d", &n, &m); int a, b; for(int i=0; i<m; i++){ scanf("%d%d", &a, &b); if(!U2.query(a, b)){ ms[U1.find(a)].push_back({a, b}); ms[U1.find(b)].push_back({b, a}); U2.update(a, b); } else if(!U1.query(a, b)){ ms[U1.find(a)].push_back({a, b}); ms[U1.find(b)].push_back({b, a}); assert(dfs(U1.find(a), -1)); } // cout << "na " << i << endl; } for(int i=1; i<=n; i++){ for(int j=0; j<(int)ms[i].size(); j++){ if(U1.find(ms[i][j].first)<U1.find(ms[i][j].second)){ printf("%d %d\n", ms[i][j].first, ms[i][j].second); } } } return 0; }

Compilation message (stderr)

pipes.cpp: In function 'int main()':
pipes.cpp:122:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:125:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |   scanf("%d%d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...