#include <stdio.h>
#include <vector>
#include <queue>
std::vector<int> AdjList[100005];
int Vis[100005];
int p[100005];
int H[100005];
int Pa[17][100005];
int mark[100005];
std::queue<std::pair<int, int>> Q;
int UF(int u)
{
return (p[u] == 0)?u:(p[u] = UF(p[u]));
}
void dfs(int u, int pa, int h)
{
// printf("dfs u%d pa%d h%d\n", u, pa, h);
Vis[u] = 1;
H[u] = h;
Pa[0][u] = pa;
int i, v, s = AdjList[u].size();
for (i = 0; i < s; i++)
{
v = AdjList[u][i];
if (v != pa)
dfs(v, u, h + 1);
}
}
int lgN;
int lca(int u, int v)
{
if (H[v] > H[u])
return lca(v, u);
int i;
if (H[u] > H[v])
{
for (i = lgN - 1; i >= 0; i--)
if (H[Pa[i][u]] > H[v])
u = Pa[i][u];
u = Pa[0][u];
}
if (u == v)
return u;
for (i = lgN - 1; i >= 0; i--)
{
if (Pa[i][u] != Pa[i][v])
{
u = Pa[i][u];
v = Pa[i][v];
}
}
return Pa[0][u];
}
void dfsMark(int u, int pa)
{
int i, v, s = AdjList[u].size();
Vis[u] = 0;
for (i = 0; i < s; i++)
{
v = AdjList[u][i];
if (v != pa)
{
dfsMark(v, u);
mark[u] += mark[v];
}
}
}
int main()
{
int N, M;
scanf("%d %d", &N, &M);
for (lgN = 0; (1 << lgN) <= N; lgN++);
int i, j, u, v, pu, pv;
int cnt = 0;
for (i = 0; i < M && cnt < N - 1; i++)
{
scanf("%d %d", &u, &v);
if ((pu = UF(u)) != (pv = UF(v)))
{
AdjList[u].push_back(v);
AdjList[v].push_back(u);
p[pu] = pv;
cnt++;
}
else
{
Q.push({u, v});
}
}
for (i = 1; i <= N; i++)
if (!Vis[i])
dfs(i, 0, 0);
for (j = 1; j < lgN; j++)
{
for (i = 1; i <= N; i++)
{
Pa[j][i] = Pa[j - 1][Pa[j - 1][i]];
}
}
while (!Q.empty())
{
u = Q.front().first;
v = Q.front().second;
Q.pop();
// printf("Q u%d v%d\n", u, v);
mark[u]++;
mark[v]++;
// printf("lca %d\n", lca(u, v));
mark[lca(u, v)] -= 2;
}
for (; i < M; i++)
{
scanf("%d %d", &u, &v);
// printf("# u%d v%d\n", u, v);
mark[u]++;
mark[v]++;
// printf("lca %d\n", lca(u, v));
mark[lca(u, v)] -= 2;
}
for (i = 1; i <= N; i++)
if (Vis[i])
dfsMark(i, 0);
for (i = 1; i <= N; i++)
{
// printf("mark[%d] = %d\n", i, mark[i]);
if (mark[i] == 0 && Pa[0][i] != 0)
printf("%d %d\n", i, Pa[0][i]);
}
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:71:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:113:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2660 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
3312 KB |
Output is correct |
2 |
Correct |
13 ms |
3200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
238 ms |
3200 KB |
Output is correct |
2 |
Correct |
528 ms |
7884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
562 ms |
4040 KB |
Output is correct |
2 |
Correct |
1330 ms |
13528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1138 ms |
6112 KB |
Output is correct |
2 |
Correct |
1610 ms |
16064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2430 ms |
13508 KB |
Output is correct |
2 |
Runtime error |
2554 ms |
23200 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4682 ms |
15952 KB |
Output is correct |
2 |
Runtime error |
4339 ms |
35008 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5006 ms |
18640 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5066 ms |
18640 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5059 ms |
17472 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |