답안 #37476

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
37476 2017-12-25T18:10:10 Z mohammad_kilani Pipes (CEOI15_pipes) C++14
100 / 100
1842 ms 8696 KB
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define oo 2000000000
const int N = 100010;
int n, m , cnt = 0 , dsu[N] , dsu2[N] , comp[N] , u , v , a ,b , depth[N];
bitset < N > bridge;
vector< pair<int,int> > g[N];
pair<int,int> parent[N];

inline int find(int u){ return (u == dsu[u] ? u : dsu[u] = find(dsu[u]));}
inline int find2(int u){ return (u == dsu2[u] ? u : dsu2[u] = find2(dsu2[u]));}

void DFS(int node,int prnt,int e,int d){
	parent[node] = make_pair(prnt,e);
	depth[node] = d;
	if(bridge[e]) dsu2[node] = node; else dsu2[node] = find2(prnt);
	for(int i=0;i<g[node].size();i++){
		if(g[node][i].first != prnt) DFS(g[node][i].first,node,g[node][i].second,d+1);
	}
}

int get(int node1,int node2){
	if(node1 == node2) return find2(node1);
	if(depth[node1] > depth[node2]){
		bridge[parent[node1].second] = false;
		return dsu2[node1] = dsu2[node2] = get(find2(parent[node1].first),node2);
	}
	bridge[parent[node2].second] = false;
	return dsu2[node1] = dsu2[node2] = get(node1,find2(parent[node2].first));
}

int main() {
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) dsu[i] = dsu2[i] = i , comp[i] = 1;
	for(int i=0;i<m;i++){
		scanf("%d%d",&u,&v);
		a = find(u);
		b = find(v);
		if(a == b){
			get(u,v);
		}
		else{
			g[u].push_back(make_pair(v,cnt));
			g[v].push_back(make_pair(u,cnt));
			bridge[cnt] = true;
			if(comp[a] > comp[b]){
				DFS(v,u,cnt,depth[u] + 1);
				comp[a] += comp[b];
				comp[b] = 0;
				dsu[b] = a;
			}
			else{
				DFS(u,v,cnt,depth[v] + 1);
				comp[b] += comp[a];
				comp[a] = 0;
				dsu[a] = b;
			}
			cnt++;
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=0;j<g[i].size();j++){
			if(i > g[i][j].first || !bridge[g[i][j].second]) continue;
			printf("%d %d\n",i,g[i][j].first);
		}
	}
	return 0;
}

Compilation message

pipes.cpp: In function 'void DFS(int, int, int, int)':
pipes.cpp:18:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[node].size();i++){
              ~^~~~~~~~~~~~~~~
pipes.cpp: In function 'int main()':
pipes.cpp:63:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j=0;j<g[i].size();j++){
               ~^~~~~~~~~~~~
pipes.cpp:34:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&n,&m);
  ~~~~~^~~~~~~~~~~~~~
pipes.cpp:37:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&u,&v);
   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2688 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 2944 KB Output is correct
2 Correct 7 ms 2944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 2844 KB Output is correct
2 Correct 119 ms 2936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 216 ms 3252 KB Output is correct
2 Correct 254 ms 3316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 376 ms 4128 KB Output is correct
2 Correct 324 ms 4336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 524 ms 6916 KB Output is correct
2 Correct 489 ms 6904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 917 ms 7500 KB Output is correct
2 Correct 838 ms 7224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1190 ms 8568 KB Output is correct
2 Correct 1178 ms 8692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1548 ms 8696 KB Output is correct
2 Correct 1509 ms 8684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1842 ms 8328 KB Output is correct
2 Correct 1683 ms 8584 KB Output is correct