답안 #407413

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
407413 2021-05-18T22:18:55 Z peuch Simurgh (IOI17_simurgh) C++17
51 / 100
228 ms 4164 KB
#include "simurgh.h"
#include<bits/stdc++.h>
using namespace std;
 
const int MAXN = 1010;
 
int n, m;
vector<int> u, v;
 
vector<int> ar[MAXN], id[MAXN];
int marc[MAXN], pai[MAXN], paiID[MAXN], prof[MAXN];
int grp[200000];
int pode[200000];
int cnt[200000];
 
int a;
 
int rep[MAXN], tam[MAXN];
 
vector<int> getPath(int a, int b);
vector<int> getST(vector<int> base);
bool uni(int a, int b);
int find(int a);
void dfs(int cur);
 
vector<int> find_roads(int _n, vector<int> _u, vector<int> _v) {
	n = _n;
	u = _u, v = _v;
	m = u.size();
	
	for(int i = 0; i < u.size(); i++){
		ar[u[i]].push_back(v[i]);
		id[u[i]].push_back(i);
		ar[v[i]].push_back(u[i]);
		id[v[i]].push_back(i);
	}
	
	vector<int> nulo;
	vector<int> tree = getST(nulo);
	
	a = count_common_roads(tree);
	
	for(int i = 0; i < tree.size(); i++)
		pode[tree[i]] = 1;
	
	dfs(0);
	
	for(int i = 0; i < m; i++){
		if(grp[i] != 0 || pode[i]) continue;
		vector<int> path = getPath(u[i], v[i]);
		
//		printf("Current cycle: %d\nPath: ", i);
//		for(int j = 0; j < path.size(); j++)
//			printf("%d ", path[j]);
//		printf("\n");
		
		int maxi = a;
		bool flag = false;
		tree.push_back(i);
		cnt[i] = a;
		for(int j = 0; j < path.size(); j++){
			if(grp[path[j]] != 0){
				if(flag) continue;
				if(grp[path[j]] == 1) continue;
				flag = true;
			}
			
			vector<int> test;
			for(int k = 0; k < tree.size(); k++)
				if(tree[k] != path[j]) test.push_back(tree[k]);
			
//			printf("\tTesting: %d\n", path[j]);
			cnt[path[j]] = count_common_roads(test);
			maxi = max(maxi, cnt[path[j]]);
		}	
		
		path.push_back(i);
		for(int j = 0; j < path.size(); j++){
			if(grp[path[j]] != 0) continue;
			if(cnt[path[j]] == maxi) grp[path[j]] = -1;
			if(cnt[path[j]] != maxi) grp[path[j]] = 1;
//			printf("Cnt[%d] = %d -> grp = %d\n", path[j], cnt[path[j]], grp[path[j]]);
		}
		tree.pop_back();
		 
	}
	
	vector<int> ans;
	for(int i = 0; i < m; i++)
		if(grp[i] != -1) ans.push_back(i);
		
//	for(int i = 0; i < ans.size(); i++){
//		printf("%d ", ans[i]);
//	}
//	printf("\n");
	
	return ans;
}
 
vector<int> getPath(int a, int b){
	vector<int> ret;
	if(prof[a] < prof[b]) swap(a, b);
	while(prof[a] > prof[b]){
		ret.push_back(paiID[a]);
		a = pai[a];
	}
	while(a != b){
		ret.push_back(paiID[a]);
		a = pai[a];
		ret.push_back(paiID[b]);
		b = pai[b];
	}
	return ret;
}
 
vector<int> getST(vector<int> base){
	vector<int> ret;
	for(int i = 0; i < n; i++){
		tam[i] = 1;
		rep[i] = i;
	}
	for(int i = 0; i < base.size(); i++)
		uni(u[base[i]], v[base[i]]);
	for(int i = 0; i < m; i++)
		if(uni(u[i], v[i])) ret.push_back(i);
	return ret;
}
 
bool uni(int a, int b){
	a = find(a);
	b = find(b);
	if(a == b) return 0;
	if(tam[a] < tam[b]) swap(a, b);
	tam[a] += tam[b];
	rep[b] = a;
	return 1;
}
 
int find(int a){
	if(a == rep[a]) return a;
	return rep[a] = find(rep[a]);
}
 
void dfs(int cur){
	for(int i = 0; i < ar[cur].size(); i++){
		int viz = ar[cur][i];
		if(!pode[id[cur][i]]) continue;
		if(viz == pai[cur]) continue;
		pai[viz] = cur;
		paiID[viz] = id[cur][i];
		prof[viz] = prof[cur] + 1;
		dfs(viz);
	}
}
/*
5 7
0 1
1 2
2 3
3 4
0 2
1 3
4 2
1 3 4 6
*/

Compilation message

simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:31:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |  for(int i = 0; i < u.size(); i++){
      |                 ~~^~~~~~~~~~
simurgh.cpp:43:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |  for(int i = 0; i < tree.size(); i++)
      |                 ~~^~~~~~~~~~~~~
simurgh.cpp:61:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |   for(int j = 0; j < path.size(); j++){
      |                  ~~^~~~~~~~~~~~~
simurgh.cpp:69:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |    for(int k = 0; k < tree.size(); k++)
      |                   ~~^~~~~~~~~~~~~
simurgh.cpp:78:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |   for(int j = 0; j < path.size(); j++){
      |                  ~~^~~~~~~~~~~~~
simurgh.cpp: In function 'std::vector<int> getST(std::vector<int>)':
simurgh.cpp:122:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |  for(int i = 0; i < base.size(); i++)
      |                 ~~^~~~~~~~~~~~~
simurgh.cpp: In function 'void dfs(int)':
simurgh.cpp:145:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 332 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 332 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 332 KB correct
13 Correct 1 ms 332 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 332 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 332 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 332 KB correct
13 Correct 1 ms 332 KB correct
14 Correct 3 ms 332 KB correct
15 Correct 3 ms 332 KB correct
16 Correct 3 ms 332 KB correct
17 Correct 3 ms 332 KB correct
18 Correct 2 ms 332 KB correct
19 Correct 3 ms 332 KB correct
20 Correct 4 ms 456 KB correct
21 Correct 3 ms 332 KB correct
22 Correct 1 ms 332 KB correct
23 Correct 1 ms 332 KB correct
24 Correct 1 ms 332 KB correct
25 Correct 1 ms 332 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 1 ms 332 KB correct
28 Correct 1 ms 332 KB correct
29 Correct 1 ms 332 KB correct
30 Correct 1 ms 332 KB correct
31 Correct 1 ms 332 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 332 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 332 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 332 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 332 KB correct
13 Correct 1 ms 332 KB correct
14 Correct 3 ms 332 KB correct
15 Correct 3 ms 332 KB correct
16 Correct 3 ms 332 KB correct
17 Correct 3 ms 332 KB correct
18 Correct 2 ms 332 KB correct
19 Correct 3 ms 332 KB correct
20 Correct 4 ms 456 KB correct
21 Correct 3 ms 332 KB correct
22 Correct 1 ms 332 KB correct
23 Correct 1 ms 332 KB correct
24 Correct 1 ms 332 KB correct
25 Correct 1 ms 332 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 1 ms 332 KB correct
28 Correct 1 ms 332 KB correct
29 Correct 1 ms 332 KB correct
30 Correct 1 ms 332 KB correct
31 Correct 1 ms 332 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 332 KB correct
34 Correct 193 ms 1812 KB correct
35 Correct 198 ms 2108 KB correct
36 Correct 133 ms 1540 KB correct
37 Correct 10 ms 472 KB correct
38 Correct 199 ms 1948 KB correct
39 Correct 187 ms 1948 KB correct
40 Correct 131 ms 1656 KB correct
41 Correct 228 ms 1936 KB correct
42 Correct 222 ms 1940 KB correct
43 Correct 11 ms 1344 KB correct
44 Correct 9 ms 1100 KB correct
45 Correct 10 ms 1100 KB correct
46 Correct 8 ms 972 KB correct
47 Correct 5 ms 716 KB correct
48 Correct 2 ms 332 KB correct
49 Correct 3 ms 460 KB correct
50 Correct 5 ms 716 KB correct
51 Correct 10 ms 1100 KB correct
52 Correct 9 ms 1100 KB correct
53 Correct 8 ms 996 KB correct
54 Correct 11 ms 1356 KB correct
55 Correct 11 ms 1100 KB correct
56 Correct 12 ms 1100 KB correct
57 Correct 11 ms 1180 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Incorrect 158 ms 4164 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 332 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 332 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 332 KB correct
13 Correct 1 ms 332 KB correct
14 Correct 3 ms 332 KB correct
15 Correct 3 ms 332 KB correct
16 Correct 3 ms 332 KB correct
17 Correct 3 ms 332 KB correct
18 Correct 2 ms 332 KB correct
19 Correct 3 ms 332 KB correct
20 Correct 4 ms 456 KB correct
21 Correct 3 ms 332 KB correct
22 Correct 1 ms 332 KB correct
23 Correct 1 ms 332 KB correct
24 Correct 1 ms 332 KB correct
25 Correct 1 ms 332 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 1 ms 332 KB correct
28 Correct 1 ms 332 KB correct
29 Correct 1 ms 332 KB correct
30 Correct 1 ms 332 KB correct
31 Correct 1 ms 332 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 332 KB correct
34 Correct 193 ms 1812 KB correct
35 Correct 198 ms 2108 KB correct
36 Correct 133 ms 1540 KB correct
37 Correct 10 ms 472 KB correct
38 Correct 199 ms 1948 KB correct
39 Correct 187 ms 1948 KB correct
40 Correct 131 ms 1656 KB correct
41 Correct 228 ms 1936 KB correct
42 Correct 222 ms 1940 KB correct
43 Correct 11 ms 1344 KB correct
44 Correct 9 ms 1100 KB correct
45 Correct 10 ms 1100 KB correct
46 Correct 8 ms 972 KB correct
47 Correct 5 ms 716 KB correct
48 Correct 2 ms 332 KB correct
49 Correct 3 ms 460 KB correct
50 Correct 5 ms 716 KB correct
51 Correct 10 ms 1100 KB correct
52 Correct 9 ms 1100 KB correct
53 Correct 8 ms 996 KB correct
54 Correct 11 ms 1356 KB correct
55 Correct 11 ms 1100 KB correct
56 Correct 12 ms 1100 KB correct
57 Correct 11 ms 1180 KB correct
58 Correct 1 ms 332 KB correct
59 Correct 1 ms 332 KB correct
60 Incorrect 158 ms 4164 KB WA in grader: NO
61 Halted 0 ms 0 KB -