#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define pii pair<int,int>
#define mpr make_pair
typedef long long ll;
const int maxn = 1e5+10;
const int mod = 1e9+7;
const int inf = 1e9+10;
int n, m, q;
struct dsu
{
int par[maxn], tp;
dsu() {memset(par, -1, sizeof par);}
int get_par(int v)
{
if(par[v] < 0) return v;
return par[v] = get_par(par[v]);
}
void merg(int u, int v)
{
u = get_par(u);
v = get_par(v);
if(u == v) return;
if(par[u] < par[v]) swap(u,v);
par[v] += par[u];
par[u] = v;
}
bool ask(int u, int v) {return (get_par(u) == get_par(v));}
} G, T;
/// G --> kole graph
/// T --> molefe haye 2hamband yali graph
vector<pii> g[maxn];
bool mark[maxn]; int la[maxn];
void pfs(int v, int p = 0)
{
mark[v] = 1;
for(auto e : g[v])
{
int u = e.F;
if(mark[u]) {la[v]++; la[u]--;}
}
for(auto e : g[v])
{
int u = e.F;
if(!mark[u]) pfs(u,v);
}
la[p]++; la[v]--;
}
vector<int> ans;
void dfs(int v, int id = -1)
{
mark[v] = 1;
for(auto e : g[v])
{
int u = e.F, ID = e.S;
if(!mark[u])
{
dfs(u,ID);
la[v] += la[u];
}
}
if(la[v] == 0 && id >= 0)
ans.push_back(id);
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
G.tp = 1; T.tp = 2;
cin>> n >> m;
vector<pii> edge;
for(int i = 1, u, v; i <= m; i++)
{
cin>> u >> v;
if(!G.ask(u,v))
{
G.merg(u,v);
edge.push_back({u,v});
}
else
T.merg(u,v);
}
int i = 0;
for(auto e : edge)
{
int u = T.get_par(e.F), v = T.get_par(e.S);
g[u].push_back({v,i});
g[v].push_back({u,i});
i++;
}
for(int i = 1; i <= n; i++)
if(!mark[i]) pfs(i);
memset(mark, 0, sizeof mark);
for(int i = 1; i <= n; i++)
if(!mark[i]) dfs(i);
for(auto x : ans)
cout<< edge[x].F <<" "<< edge[x].S <<"\n";
}
/*
10 11
1 7
1 8
1 6
2 8
6 7
5 8
2 5
2 3
2 4
3 4
10 9
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4352 KB |
Output is correct |
2 |
Correct |
3 ms |
4352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4864 KB |
Output is correct |
2 |
Correct |
6 ms |
4864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
10208 KB |
Output is correct |
2 |
Correct |
105 ms |
9848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
173 ms |
13280 KB |
Output is correct |
2 |
Correct |
210 ms |
16376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
290 ms |
13808 KB |
Output is correct |
2 |
Runtime error |
266 ms |
20216 KB |
Memory limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
360 ms |
10088 KB |
Output is correct |
2 |
Runtime error |
331 ms |
27880 KB |
Memory limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
590 ms |
36648 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
789 ms |
53868 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
973 ms |
65384 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1214 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |