# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
457715 |
2021-08-07T10:15:59 Z |
vanic |
Pipes (CEOI15_pipes) |
C++14 |
|
5000 ms |
21040 KB |
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cassert>
using namespace std;
const int maxn=1e5+5;
multiset < pair < int, int > > ms[maxn];
struct union_find{
int parent[maxn];
union_find(){
for(int i=0; i<maxn; i++){
parent[i]=i;
}
}
int find(int x){
if(parent[x]==x){
return x;
}
return parent[x]=find(parent[x]);
}
void update(int x, int y){
x=find(x);
y=find(y);
if(ms[x].size()>ms[y].size()){
swap(x, y);
}
parent[x]=y;
pair < int, int > p;
while(!ms[x].empty()){
ms[y].insert(*ms[x].begin());
ms[x].erase(ms[x].begin());
}
}
bool query(int x, int y){
return find(x)==find(y);
}
};
struct union_find2{
int parent[maxn];
int sz[maxn];
union_find2(){
for(int i=0; i<maxn; i++){
parent[i]=i;
sz[i]=1;
}
}
int find(int x){
if(x==parent[x]){
return x;
}
return parent[x]=find(parent[x]);
}
void update(int x, int y){
x=find(x);
y=find(y);
if(sz[x]>sz[y]){
swap(x, y);
}
parent[x]=y;
sz[y]+=sz[x];
}
bool query(int x, int y){
return find(x)==find(y);
}
};
union_find U1;
union_find2 U2;
vector < int > put;
vector < pair < int, int > > edge;
bool bio[maxn];
bool dfs(int x, int prosl){
bool p=0;
// cout << x << endl;
if(bio[x]){
int ind1, ind2;
for(int i=0; i<(int)put.size(); i++){
if(put[i]==x){
p=1;
ind1=i;
ind2=put.size()-1;
// cout << "rez " << ms[put[i]].size() << ' ';
ms[put[i]].erase(edge[ind1]);
ms[put[i]].erase({edge[ind2].second, edge[ind2].first});
// cout << ms[put[i]].size() << '\n';
}
else if(p){
ind1=i;
ind2=i-1;
// cout << "rez " << ms[put[i]].size() << ' ';
ms[put[i]].erase(edge[ind1]);
ms[put[i]].erase({edge[ind2].second, edge[ind2].first});
// cout << ms[put[i]].size() << '\n';
// cout << "upd " << put[i] << ' ' << put[i-1] << endl;
U1.update(put[i], put[i-1]);
}
}
return 1;
}
bio[x]=1;
put.push_back(x);
for(auto i=ms[x].begin(); i!=ms[x].end(); i++){
if(!p && U1.find(i->second)==prosl){
p=1;
}
else{
edge.push_back(*i);
if(dfs(U1.find(i->second), x)){
bio[x]=0;
put.pop_back();
edge.pop_back();
return 1;
}
edge.pop_back();
}
}
put.pop_back();
bio[x]=0;
return 0;
}
int main(){
int n, m;
scanf("%d%d", &n, &m);
int a, b;
for(int i=0; i<m; i++){
scanf("%d%d", &a, &b);
if(!U2.query(a, b)){
ms[U1.find(a)].insert({a, b});
ms[U1.find(b)].insert({b, a});
U2.update(a, b);
}
else if(!U1.query(a, b)){
ms[U1.find(a)].insert({a, b});
ms[U1.find(b)].insert({b, a});
assert(dfs(U1.find(a), -1));
}
// cout << "na " << i << endl;
}
for(int i=1; i<=n; i++){
for(auto j=ms[i].begin(); j!=ms[i].end(); j++){
if(j->first<j->second){
printf("%d %d\n", j->first, j->second);
}
}
}
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:135:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:138:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6092 KB |
Output is correct |
2 |
Incorrect |
5 ms |
6092 KB |
Wrong number of edges |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
6404 KB |
Output is correct |
2 |
Correct |
40 ms |
6348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
246 ms |
6348 KB |
Output is correct |
2 |
Incorrect |
131 ms |
6348 KB |
Wrong number of edges |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
614 ms |
6608 KB |
Output is correct |
2 |
Incorrect |
298 ms |
6660 KB |
Wrong number of edges |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2847 ms |
7356 KB |
Output is correct |
2 |
Runtime error |
1370 ms |
21040 KB |
Memory limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5063 ms |
9744 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5098 ms |
10300 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5091 ms |
11624 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5092 ms |
11692 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5093 ms |
11216 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |