# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
246458 |
2020-07-09T11:25:31 Z |
dantoh000 |
Pipes (CEOI15_pipes) |
C++14 |
|
5000 ms |
13156 KB |
#include <utility>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <unordered_set>
using namespace std;
typedef pair<int,int> ii;
int n,m;
int d[100005], pa[100005], sz[100005];
vector<int> G[100005];
unordered_set<int> bad[100005];
struct ufds{
int p[100005];
ufds(){
for (int i = 0; i <= 100000; i++) p[i] = i;
}
inline int find(int x){
return p[x] == x ? x : p[x] = find(p[x]);
}
inline void un(int x, int y){
p[x] = y;
}
} u1 = ufds(), u2 = ufds();
void dfs(int u, int p){
///join at side bad
u2.p[u] = u;
pa[u] = p;
for (auto v : G[u]){
if (v == p) continue;
else{
d[v] = d[u]+1;
dfs(v,u);
}
}
}
int main(){
scanf("%d%d",&n,&m);
for (int i = 0; i < m; i++){
int u,v;
scanf("%d%d",&u,&v);
int pu = u1.find(u), pv = u1.find(v);
//printf("%d -> %d, %d -> %d\n",u,pu,v,pv);
if (pu == pv){
//printf("update path %d %d\n",u,v);
///make all edges along this path bad
///all edges along path are only changed once, amortised O()
u = u2.find(u), v = u2.find(v);
while (u != v){
if (d[u] < d[v]) swap(u,v);
bad[min(u,pa[u])].insert(max(u,pa[u]));
//printf("bad %d %d\n",u,pa[u]);
u2.un(u,pa[u]);
u = pa[u];
u = u2.find(u);
}
}
else{
//printf("join %d %d\n",u,v);
///join the two components
if (sz[pu] < sz[pv]){
swap(pu,pv);
swap(u,v);
}
///join small v to big u
u1.un(pv,pu);
sz[pu] += sz[pv];
///add edge
G[u].push_back(v);
G[v].push_back(u);
///restart dfs
d[v] = d[u]+1;
dfs(v,u);
}
}
for (int u = 1; u <= n ;u++){
for (auto v : G[u]){
if (v > u && bad[u].find(v) == bad[u].end()){
printf("%d %d\n",u,v);
}
}
}
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:39: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:42: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 |
10 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
9464 KB |
Output is correct |
2 |
Correct |
39 ms |
9344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
9216 KB |
Output is correct |
2 |
Correct |
158 ms |
9216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
612 ms |
9824 KB |
Output is correct |
2 |
Correct |
371 ms |
9980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3126 ms |
11192 KB |
Output is correct |
2 |
Correct |
2175 ms |
12100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5065 ms |
12700 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5069 ms |
12720 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5053 ms |
13156 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5058 ms |
13048 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5053 ms |
12988 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |