This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> p,r;
DSU(int n) :p(n),r(n) {
for (int i=0;i<n;i++)p[i]=i;
}
int get(int i) {
if (p[i]!=i && p[i]!=p[p[i]]) p[i]=get(p[i]);
return p[i];
}
bool unite(int a,int b) {
a=get(a),b=get(b);
if (a==b)return false;
if (r[a]<r[b])swap(a,b);
p[b]=a;
if (r[a]==r[b])r[a]++;
return true;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;cin>>n>>m;
DSU d1(n), d2(n);
vector<vector<int>> g(n);
for (int i=0;i<m;i++) {
int a,b;cin>>a>>b;a--;b--;
if (d1.unite(a,b)) {
g[a].push_back(b);
g[b].push_back(a);
}
else {
d2.unite(a,b);
}
}
vector<vector<int>> lft(n, vector<int>(18, -1));
bitset<100001> vis;
vector<int> d(n,0);
function<void(int,int)> dfs=[&](int node, int pv) {
vis[node]=true;
for (int x:g[node]) {
if (x==pv)continue;
lft[x][0]=node;
d[x]=d[node]+1;
dfs(x, node);
}
};
for (int i=0;i<n;i++) {
if (!vis[i]) dfs(i,-1);
}
for (int j=1;j<18;j++) {
for (int i=0;i<n;i++) {
lft[i][j]=lft[i][j-1];
if (lft[i][j] != -1) {
lft[i][j]=lft[lft[i][j]][j-1];
}
}
}
vector<vector<int>> bkt(n);
for (int i=0;i<n;i++) {
if (d2.get(i) == i) continue;
bkt[d2.get(i)].push_back(i);
}
function<int(int,int)> jmp=[&](int a, int steps) -> int {
int cnt=0;
while (steps) {
if ((steps&1)==1) {
a=lft[a][cnt];
if (a==-1)return -1;
}
steps>>=1;cnt++;
}
return a;
};
function<int(int,int)> lca=[&](int a,int b) -> int {
if (d[a]<d[b])swap(a,b);
a=jmp(a,d[a]-d[b]);
if (a==b)return a;
for (int i=17;i>-1;--i) {
if (lft[a][i] != lft[b][i]) {
a=lft[a][i];
b=lft[b][i];
}
}
return lft[a][0];
};
vector<int> dp(n, 0);
for (int i=0;i<n;i++) {
if (bkt[i].size() < 1) continue;
int st = i;
for (int x:bkt[i]) {
int l=lca(st, x);
dp[st]++;
dp[x]++;
dp[l] -= 2;
}
}
for (int i=0;i<n;i++) vis[i]=0;
function<void(int)> dfs2=[&](int node) {
vis[node]=true;
for (int x:g[node]) {
if (vis[x])continue;
dfs2(x);
dp[node]+=dp[x];
}
};
for (int i=0;i<n;i++) {
if (!vis[i])dfs2(i);
}
for (int i=0;i<n;i++) {
if (dp[i]==0 && lft[i][0] != -1) {
cout<<i+1<<" "<<lft[i][0]+1<<"\n";
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |