#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#define DIMN 100005
using namespace std;
int fth[DIMN],fth2[DIMN],sum[DIMN];
vector <int> v[DIMN];
int lvl[DIMN];
bool f[DIMN];
int d[18][2*DIMN];
int logg[2*DIMN];
int elem;
int root (int nod){
int init = nod,aux;
while (fth[nod]>0){
nod = fth[nod];
}
while (fth[init]>0){ /// amortizare
aux = fth[init];
fth[init] = nod;
init = aux;
}
return nod;
}
int root2 (int nod){
int init = nod,aux;
while (fth2[nod]>0){
nod = fth2[nod];
}
while (fth2[init]>0){ /// amortizare
aux = fth2[init];
fth2[init] = nod;
init = aux;
}
return nod;
}
void dfs (int nod,int tata){
int i,vecin;
f[nod] = 1;
for (i=0;i<v[nod].size();i++){
vecin = v[nod][i];
if (vecin!=tata){
dfs(vecin , nod);
sum[nod]+=sum[vecin];
if (!sum[vecin]){
printf ("%d %d\n",nod,vecin);
}
}
}
}
void dfs_lca (int nod,int niv,int tt){
int i,vecin;
d[0][nod] = tt;
lvl[nod] = niv;
for (i=0;i<v[nod].size();i++){
vecin=v[nod][i];
if (vecin!=tt){
dfs_lca(vecin,niv+1,nod);
}
}
}
int kth (int nod , int k){
while (k && nod!=0){
nod=d[logg[k]][nod];
k=k-(1<<logg[k]);
}
return nod;
}
int query (int x,int y){
int st,dr,mid;
if (lvl[x] < lvl[y])
swap(x,y);
if (lvl[x] > lvl[y])
x = kth( x , lvl[x] - lvl[y] );
st = 0;
dr = lvl[x];
while (st <= dr){
mid = (st + dr)/2;
if (kth(x,mid) == kth(y,mid))
dr = mid - 1;
else st = mid + 1;
}
return kth(x,st);
}
int main()
{
//freopen ("a.in" , "r" , stdin);
int n,m,i,x,y,rx,ry;
scanf ("%d%d",&n,&m);
for (i=1;i<=n;i++)
fth[i] = fth2[i] = -1;
for (i=1;i<=m;i++){
scanf ("%d%d",&x,&y);
rx = root(x);
ry = root(y);
if (rx != ry){ /// sunt in cc diferite
v[x].push_back(y);
v[y].push_back(x); /// mai multi arbori
if (fth[rx] < fth[ry]){
fth[rx]+=fth[ry];
fth[ry] = rx;
}
else {
fth[ry]+=fth[rx];
fth[rx] = ry;
}
}
else {
rx = root2(x);
ry = root2(y);
if (rx!=ry){ /// unesti in fth2
if (fth2[rx] < fth2[ry]){
fth2[rx]+=fth2[ry];
fth2[ry] = rx;
}
else {
fth2[ry]+=fth2[rx];
fth2[rx] = ry;
}
}
}
}
/// faci lca cu stramosi ca sa economisesti memorie
for (i=1;i<=n;i++){
if (!f[i]){
dfs_lca (i,0,0);
}
}
memset ( f, 0 ,sizeof(f) );
for (i=2;i<=2*n;i++)
logg[i] = logg[i/2] + 1;
for (int powe=1;powe<=logg[elem];powe++){
for (i=1;i + (1<<powe)-1 <=elem;i++){
d[powe][i] = d[powe-1][d[powe-1][i]];
}
}
for (i=1;i<=n;i++){ /// daca ai o muchie in fth2 , stergi din fth
if (fth2[i]>0){
int lca = query (i , fth2[i]);
sum[i]++;
sum[fth2[i]]++;
sum[lca]-=2;
}
}
for (i=1;i<=n;i++){
if (!f[i])
dfs(i,0);
}
return 0;
}
Compilation message
pipes.cpp: In function 'void dfs(int, int)':
pipes.cpp:41:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<v[nod].size();i++){
~^~~~~~~~~~~~~~
pipes.cpp: In function 'void dfs_lca(int, int, int)':
pipes.cpp:56:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<v[nod].size();i++){
~^~~~~~~~~~~~~~
pipes.cpp: In function 'int main()':
pipes.cpp:92:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d%d",&n,&m);
~~~~~~^~~~~~~~~~~~~~
pipes.cpp:96:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d%d",&x,&y);
~~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2808 KB |
Output is correct |
2 |
Incorrect |
4 ms |
2808 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
572 ms |
3076 KB |
Output is correct |
2 |
Incorrect |
576 ms |
3064 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
447 ms |
3152 KB |
Output is correct |
2 |
Incorrect |
407 ms |
3192 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4224 ms |
3300 KB |
Output is correct |
2 |
Incorrect |
4167 ms |
3288 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5046 ms |
3780 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5088 ms |
6008 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5080 ms |
6520 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5008 ms |
7416 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5049 ms |
7416 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5053 ms |
7168 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |