#include <bits/stdc++.h>
#define N 100005
using namespace std;
int par[N][2];
int find(int a,int val){
if(par[a][val] == a)return a;
return par[a][val] = find(par[a][val],val);
}
vector<pair<int,int>> adj[N];
bool merge(int a,int b,int val){
int tmp1 = a,tmp2 = b;
a = find(a,val);
b = find(b,val);
if(a == b)return 0;
par[a][val] = b;
adj[tmp1].push_back({tmp2,val});
adj[tmp2].push_back({tmp1,val});
return 1;
}
int n;
vector<bool> visited;
vector<int> tin, low;
int timer;
void IS_BRIDGE(int v,int a){
cout << v << " " << a << endl;
}
void dfs(int v, int p = -1) {
visited[v] = true;
tin[v] = low[v] = timer++;
for (auto x : adj[v]) {
int to = x.first;
if (to == p) continue;
if (visited[to]) {
low[v] = min(low[v], tin[to]);
} else {
dfs(to, v);
low[v] = min(low[v], low[to]);
if (low[to] > tin[v] && !x.second)
IS_BRIDGE(v, to);
}
}
}
void find_bridges() {
timer = 0;
visited.assign(N, false);
tin.assign(N, -1);
low.assign(N, -1);
for (int i = 1; i <= n; ++i) {
if (!visited[i])
dfs(i);
}
}
void solve(){
int m;
cin >> n >> m;
for(int i=1;i<=n;i++){
par[i][0] = par[i][1] = i;
}
int l,r;
for(int i=1;i<=m;i++){
cin >> l >> r;
if(!merge(l,r,0))
merge(l,r,1);
}
find_bridges();
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3436 KB |
Output is correct |
2 |
Incorrect |
3 ms |
3436 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
3948 KB |
Output is correct |
2 |
Incorrect |
8 ms |
3820 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
3948 KB |
Output is correct |
2 |
Incorrect |
140 ms |
3832 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
213 ms |
4592 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
343 ms |
6380 KB |
Output is correct |
2 |
Correct |
271 ms |
5740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
491 ms |
11612 KB |
Output is correct |
2 |
Incorrect |
467 ms |
8784 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
712 ms |
12920 KB |
Output is correct |
2 |
Incorrect |
714 ms |
9528 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
946 ms |
15016 KB |
Output is correct |
2 |
Incorrect |
925 ms |
10992 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1157 ms |
15016 KB |
Output is correct |
2 |
Runtime error |
1136 ms |
63976 KB |
Memory limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1352 ms |
14412 KB |
Output is correct |
2 |
Runtime error |
1330 ms |
65536 KB |
Memory limit exceeded |