Submission #450340

# Submission time Handle Problem Language Result Execution time Memory
450340 2021-08-02T14:51:39 Z mp007mp Pipes (CEOI15_pipes) C++14
100 / 100
1364 ms 13136 KB
#include<bits/stdc++.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define S second 
#define F first
#define all(x) x.begin(),x.end()
#define debug(x) cout<<#x<<" = "<<x<<"\n";
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
int mod = 1e9+7;
int inf = 2e9;
ll linf = 1000000ll*10000*10000;
const int maxn = 1e5+20;
int ldr[2][maxn],cnt[maxn],h[maxn];
bitset<maxn> mark;
vector<int> adj[maxn];
int dgt(int v,int det){
	return (ldr[det][v] == v)?v:ldr[det][v] = dgt(ldr[det][v],det);
}
inline bool uni(int u,int v,int det){
	u = dgt(u,det);
	v = dgt(v,det);
	if(u == v)return false;
	ldr[det][v] = u;
	return true;
}
void dfs(int v){
	mark[v] = 1;
	for(int u:adj[v]){
		if(mark[u])continue;
		h[u] = h[v]+1;
		dfs(u);
	}
}
void sec_dfs(int v){
	mark[v] = 1;
	for(int u:adj[v]){
		if(mark[u])continue;
		sec_dfs(u);
		if(cnt[u]<=1)cout<<u<<" "<<v<<"\n";
		cnt[v] += cnt[u];
	}
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
    	ldr[0][i] = ldr[1][i] = i;
    }
    for(int i=0;i<m;i++){
    	int u,v;
    	cin>>u>>v;
    	if(!uni(u,v,0) && !uni(u,v,1)){
    		continue;
    	}
    	adj[v].push_back(u);
    	adj[u].push_back(v);
    }
    for(int i=1;i<=n;i++){
    	if(!mark[i])dfs(i);
    }
    mark.reset();
    for(int v=1;v<=n;v++){
	    for(int u:adj[v]){
	    	if(h[u]>h[v])continue;
	    	cnt[v]++;
	    	cnt[u]--;
	    }
    }
    for(int i=1;i<=n;i++){
    	if(!mark[i])sec_dfs(i);
    }
    return 0;
}

Compilation message

pipes.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    3 | #pragma GCC optimization ("O3")
      | 
pipes.cpp:4: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    4 | #pragma GCC optimization ("unroll-loops")
      |
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3148 KB Output is correct
2 Correct 6 ms 2848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 124 ms 2968 KB Output is correct
2 Correct 128 ms 2832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 190 ms 3636 KB Output is correct
2 Correct 227 ms 3240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 324 ms 5080 KB Output is correct
2 Correct 288 ms 4804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 459 ms 9864 KB Output is correct
2 Correct 392 ms 6972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 708 ms 11000 KB Output is correct
2 Correct 683 ms 8448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 908 ms 12960 KB Output is correct
2 Correct 868 ms 8900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1112 ms 13136 KB Output is correct
2 Correct 1064 ms 8864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1351 ms 12440 KB Output is correct
2 Correct 1364 ms 10052 KB Output is correct