Submission #669571

#TimeUsernameProblemLanguageResultExecution timeMemory
669571Iliya_Pipes (CEOI15_pipes)C++17
0 / 100
921 ms65536 KiB
// IN THE NAME OF GOD #include<bits/stdc++.h> #pragma GCC optimize ("O2,unroll-loops") #define endl '\n'; #define file_reading freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); using namespace std; typedef long long ll; typedef long double dll; typedef unsigned long long ull; // Variables const int B = 1e5+7; const int BB = 6e6+7;; pair<int,int> v[BB]; int par1[B]; int siz1[B]; int par2[B]; int siz2[B]; int n,m,a,b,one,two,u; vector<pair<int,int>> g[B]; bitset<B> seen; bool ans[BB]; pair<int,int> dat[B]; int father[B]; int val[B]; // main functions int get_root1(int v){ return (par1[v] == v ? v : par1[v] = get_root1(par1[v])); } int get_root2(int v){ return (par2[v] == v ? v : par2[v] = get_root2(par2[v])); } bool merge1(int v, int u){ one = get_root1(v); two = get_root1(u); if (one == two){return 0;} if (siz1[one] > siz1[two]){swap(one,two);} siz1[two] += siz1[one]; par1[one] = two; return 1; } bool merge2(int v, int u){ one = get_root2(v); two = get_root2(u); if (one == two){return 0;} if (siz2[one] > siz2[two]){swap(one,two);} siz2[two] += siz2[one]; par2[one] = two; return 1; } void dfs(int v){ seen[v] = 1; vector<pair<int,int>> check; for(auto cur : g[v]){ u = cur.first; if (seen[u] == 0){ father[u] = v; check.push_back(cur); dfs(u); } else if (father[u] != v && father[v] != u){ dat[v].first++; dat[u].second++; } } for(auto cur : check){ u = cur.first; if (val[u] == 0){ans[cur.second] = 1;} dat[v].first += dat[u].first; dat[v].second += dat[u].second; } val[v] = dat[v].first - dat[v].second; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i=1; i<=n; i++){siz1[i] = siz2[i] = 1; par1[i] = par2[i] = i;} for(int i=1; i<=m; i++){ cin >> v[i].first >> v[i].second; if (merge1(v[i].first,v[i].second)){ g[v[i].first].push_back({v[i].second,i}); g[v[i].second].push_back({v[i].first,i}); } else if (merge2(v[i].first,v[i].second)){ g[v[i].first].push_back({v[i].second,i}); g[v[i].second].push_back({v[i].first,i}); } } for(int i=1; i<=n; i++){ if (seen[i] == 0){ father[i] = 0; dfs(i); } } for(int i=1; i<=m; i++){ if (ans[i]){ cout << v[i].first << " " << v[i].second << endl; } } return 0; }
#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...