답안 #131604

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
131604 2019-07-17T10:19:40 Z tmwilliamlin168 Simurgh (IOI17_simurgh) C++14
100 / 100
980 ms 8112 KB
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;

//#define DBG
#define dbgn(x) dbg(x, #x)
#ifdef DBG
template<typename T>
void dbg(T x, string s="") {
	cout << s << " " << x << endl;
}
template<typename T>
void dbg(vector<T> v, string s="") {
	cout << s << endl;
	for(int i : v)
		cout << i << " ";
	cout << endl;
}
#else
template<typename T>
void dbg(T x, string s="") {}
template<typename T>
void dbg(vector<T> v, string s="") {}
#endif

const int mxN=500, mxM=mxN*(mxN-1)/2;
int n, tin[mxN], low[mxN], dt=1, b[mxM], bs, st[mxM], sth, t[mxN], p[mxN];
vector<int> eu, ev, adj[mxN], ans;
bool c[mxN];

void dfs1(int u=0, int p=-1) {
	tin[u]=low[u]=dt++;
	for(int e : adj[u]) {
		int v=eu[e]^ev[e]^u;
		if(!tin[v]) {
			st[sth++]=e;
			dfs1(v, u);
			low[u]=min(low[v], low[u]);
			if(low[v]>=tin[u]) {
				do {
					b[st[--sth]]=bs;
				} while(st[sth]^e);
				++bs;
			}
		} else if(v^p&&tin[v]<tin[u]) {
			st[sth++]=e;
			low[u]=min(tin[v], low[u]);
		}
	}
}

int find(int x) {
	return x^p[x]?p[x]=find(p[x]):x;
}

bool join(int x, int y) {
	if((x=find(x))==(y=find(y)))
		return 0;
	p[x]=y;
	return 1;
}

void fst(vector<int> w, vector<int> &r) {
	for(int i=0; i<eu.size(); ++i)
		if(w[i]&&join(eu[i], ev[i]))
			r.push_back(i);
}

void dc(vector<int> &a, vector<int> &b, vector<int> &c, int l, int r, int s1, int s2) {
	if(s2==s1)
		return;
	if(l==r) {
		c.push_back(b[l]);
		return;
	}
	int m=(l+r)/2;
	vector<int> d=a;
	for(int i=l; i<=m; ++i)
		d[i]=b[i];
	int sl=count_common_roads(d);
	dc(a, b, c, l, m, s1, sl);
	dc(a, b, c, m+1, r, s1, s2-sl+s1);
}

void dfs2(int u=0, int pe=-1) {
	dbg(u, "d2");
	vector<int> v1, v2, r;
	for(int e : adj[u]) {
		int v=eu[e]^ev[e]^u;
		if(c[v])
			continue;
		if(t[v]<0) {
			v2.push_back(e);
			t[v]=e;
		} else
			v1.push_back(e);
	}
	if(v1.size()) {
		dbg(v1, "pv1");
		vector<int> w(eu.size(), 1);
		for(int e : v1) {
			w[e]=0;
			w[t[eu[e]^ev[e]^u]]=0;
		}
		iota(p, p+n, 0);
		vector<int> s1;
		for(int i=0; i<v1.size(); ++i) {
			s1.push_back(t[eu[v1[i]]^ev[v1[i]]^u]);
			join(eu[s1.back()], ev[s1.back()]);
		}
		for(int e : ans) {
			s1.push_back(e);
			join(eu[e], ev[e]);
		}
		fst(w, s1);
		vector<int> s2=v1;
		s2.insert(s2.end(), s1.begin()+v1.size(), s1.end());
		dbg(s1, "s1");
		dbg(s2, "s2");
		dc(s1, v1, r, 0, v1.size()-1, count_common_roads(s1), count_common_roads(s2));
		dbg(r, "r");
	}
	if(v2.size()) {
		dbg(v2, "pv2");
		vector<int> w(eu.size(), 1);
		for(int e : adj[u])
			w[e]=0;
		iota(p, p+n, 0);
		vector<int> s;
		fst(w, s);
		dbg(s, "s1");
		for(int e : r)
			if(join(eu[e], ev[e]))
				s.push_back(e);
		if(~pe&&join(eu[pe], ev[pe]))
			s.push_back(pe);
		for(int e : adj[u])
			if(join(eu[e], ev[e]))
				s.push_back(e);
		dbg(s, "s2");
		int bc=count_common_roads(s);
		dbgn(bc);
		sort(v2.begin(), v2.end(), [](const int &i, const int &j) {
			return b[i]<b[j];
		});
		for(int i=0, j=0; i<v2.size(); ) {
			dbgn(i);
			for(; j<v2.size()&&b[v2[j]]==b[v2[i]]; ++j);
			int o;
			for(int e : s)
				if(b[e]==b[v2[i]]&&(eu[e]==u||ev[e]==u))
					o=e;
			s.erase(find(s.begin(), s.end(), o));
			dbg(o, "er");
			vector<int> a;
			int cm=bc;
			for(; i<j; ++i) {
				s.push_back(v2[i]);
				int cc=count_common_roads(s);
				if(cc>cm) {
					cm=cc;
					a=vector<int>();
				}
				if(cc==cm)
					a.push_back(v2[i]);
				s.pop_back();
			}
			dbgn(cm);
			dbg(a, "on");
			r.insert(r.end(), a.begin(), a.end());
			s.push_back(o);
		}
	}
	for(int e : r) {
		ans.push_back(e);
		c[eu[e]^ev[e]^u]=1;
	}
	for(int e : r)
		dfs2(eu[e]^ev[e]^u, e);
}

vector<int> find_roads(int n, vector<int> u, vector<int> v) {
	::n=n;
	eu=u;
	ev=v;
	for(int i=0; i<u.size(); ++i) {
		adj[u[i]].push_back(i);
		adj[v[i]].push_back(i);
	}
	dfs1();
	c[0]=1;
	memset(t, -1, 4*n);
	dfs2();
	return ans;
}

Compilation message

simurgh.cpp: In function 'void fst(std::vector<int>, std::vector<int>&)':
simurgh.cpp:64:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<eu.size(); ++i)
               ~^~~~~~~~~~
simurgh.cpp: In function 'void dfs2(int, int)':
simurgh.cpp:107:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0; i<v1.size(); ++i) {
                ~^~~~~~~~~~
simurgh.cpp:146:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0, j=0; i<v2.size(); ) {
                     ~^~~~~~~~~~
simurgh.cpp:148:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(; j<v2.size()&&b[v2[j]]==b[v2[i]]; ++j);
          ~^~~~~~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:186:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<u.size(); ++i) {
               ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 380 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 380 KB correct
5 Correct 2 ms 376 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 376 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 376 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 380 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 380 KB correct
5 Correct 2 ms 376 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 376 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 4 ms 376 KB correct
15 Correct 2 ms 376 KB correct
16 Correct 3 ms 376 KB correct
17 Correct 4 ms 376 KB correct
18 Correct 3 ms 376 KB correct
19 Correct 4 ms 376 KB correct
20 Correct 3 ms 348 KB correct
21 Correct 3 ms 376 KB correct
22 Correct 3 ms 376 KB correct
23 Correct 3 ms 376 KB correct
24 Correct 3 ms 376 KB correct
25 Correct 2 ms 376 KB correct
26 Correct 3 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 3 ms 376 KB correct
29 Correct 2 ms 380 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 3 ms 424 KB correct
32 Correct 4 ms 376 KB correct
33 Correct 3 ms 376 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 380 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 380 KB correct
5 Correct 2 ms 376 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 376 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 4 ms 376 KB correct
15 Correct 2 ms 376 KB correct
16 Correct 3 ms 376 KB correct
17 Correct 4 ms 376 KB correct
18 Correct 3 ms 376 KB correct
19 Correct 4 ms 376 KB correct
20 Correct 3 ms 348 KB correct
21 Correct 3 ms 376 KB correct
22 Correct 3 ms 376 KB correct
23 Correct 3 ms 376 KB correct
24 Correct 3 ms 376 KB correct
25 Correct 2 ms 376 KB correct
26 Correct 3 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 3 ms 376 KB correct
29 Correct 2 ms 380 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 3 ms 424 KB correct
32 Correct 4 ms 376 KB correct
33 Correct 3 ms 376 KB correct
34 Correct 113 ms 2164 KB correct
35 Correct 107 ms 2192 KB correct
36 Correct 76 ms 1684 KB correct
37 Correct 19 ms 376 KB correct
38 Correct 108 ms 2272 KB correct
39 Correct 85 ms 1792 KB correct
40 Correct 64 ms 1588 KB correct
41 Correct 14 ms 2040 KB correct
42 Correct 14 ms 2040 KB correct
43 Correct 52 ms 1272 KB correct
44 Correct 46 ms 1016 KB correct
45 Correct 51 ms 1144 KB correct
46 Correct 41 ms 1016 KB correct
47 Correct 24 ms 632 KB correct
48 Correct 8 ms 380 KB correct
49 Correct 14 ms 504 KB correct
50 Correct 26 ms 632 KB correct
51 Correct 53 ms 1144 KB correct
52 Correct 46 ms 1116 KB correct
53 Correct 43 ms 1016 KB correct
54 Correct 54 ms 1400 KB correct
55 Correct 54 ms 1144 KB correct
56 Correct 54 ms 1144 KB correct
57 Correct 54 ms 1144 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 507 ms 5164 KB correct
4 Correct 934 ms 7476 KB correct
5 Correct 941 ms 7588 KB correct
6 Correct 56 ms 7316 KB correct
7 Correct 234 ms 7392 KB correct
8 Correct 278 ms 7336 KB correct
9 Correct 931 ms 7540 KB correct
10 Correct 944 ms 7820 KB correct
11 Correct 938 ms 7444 KB correct
12 Correct 938 ms 8112 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 529 ms 7724 KB correct
15 Correct 921 ms 7908 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 380 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 380 KB correct
5 Correct 2 ms 376 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 376 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 4 ms 376 KB correct
15 Correct 2 ms 376 KB correct
16 Correct 3 ms 376 KB correct
17 Correct 4 ms 376 KB correct
18 Correct 3 ms 376 KB correct
19 Correct 4 ms 376 KB correct
20 Correct 3 ms 348 KB correct
21 Correct 3 ms 376 KB correct
22 Correct 3 ms 376 KB correct
23 Correct 3 ms 376 KB correct
24 Correct 3 ms 376 KB correct
25 Correct 2 ms 376 KB correct
26 Correct 3 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 3 ms 376 KB correct
29 Correct 2 ms 380 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 3 ms 424 KB correct
32 Correct 4 ms 376 KB correct
33 Correct 3 ms 376 KB correct
34 Correct 113 ms 2164 KB correct
35 Correct 107 ms 2192 KB correct
36 Correct 76 ms 1684 KB correct
37 Correct 19 ms 376 KB correct
38 Correct 108 ms 2272 KB correct
39 Correct 85 ms 1792 KB correct
40 Correct 64 ms 1588 KB correct
41 Correct 14 ms 2040 KB correct
42 Correct 14 ms 2040 KB correct
43 Correct 52 ms 1272 KB correct
44 Correct 46 ms 1016 KB correct
45 Correct 51 ms 1144 KB correct
46 Correct 41 ms 1016 KB correct
47 Correct 24 ms 632 KB correct
48 Correct 8 ms 380 KB correct
49 Correct 14 ms 504 KB correct
50 Correct 26 ms 632 KB correct
51 Correct 53 ms 1144 KB correct
52 Correct 46 ms 1116 KB correct
53 Correct 43 ms 1016 KB correct
54 Correct 54 ms 1400 KB correct
55 Correct 54 ms 1144 KB correct
56 Correct 54 ms 1144 KB correct
57 Correct 54 ms 1144 KB correct
58 Correct 2 ms 376 KB correct
59 Correct 2 ms 376 KB correct
60 Correct 507 ms 5164 KB correct
61 Correct 934 ms 7476 KB correct
62 Correct 941 ms 7588 KB correct
63 Correct 56 ms 7316 KB correct
64 Correct 234 ms 7392 KB correct
65 Correct 278 ms 7336 KB correct
66 Correct 931 ms 7540 KB correct
67 Correct 944 ms 7820 KB correct
68 Correct 938 ms 7444 KB correct
69 Correct 938 ms 8112 KB correct
70 Correct 2 ms 376 KB correct
71 Correct 529 ms 7724 KB correct
72 Correct 921 ms 7908 KB correct
73 Correct 2 ms 376 KB correct
74 Correct 947 ms 7612 KB correct
75 Correct 923 ms 7496 KB correct
76 Correct 311 ms 2924 KB correct
77 Correct 951 ms 7652 KB correct
78 Correct 980 ms 7632 KB correct
79 Correct 957 ms 7672 KB correct
80 Correct 933 ms 7424 KB correct
81 Correct 50 ms 6264 KB correct
82 Correct 929 ms 7968 KB correct
83 Correct 518 ms 4256 KB correct
84 Correct 556 ms 4896 KB correct
85 Correct 506 ms 4408 KB correct
86 Correct 352 ms 3160 KB correct
87 Correct 244 ms 2496 KB correct
88 Correct 216 ms 2056 KB correct
89 Correct 190 ms 1836 KB correct
90 Correct 166 ms 1600 KB correct
91 Correct 51 ms 544 KB correct
92 Correct 29 ms 376 KB correct
93 Correct 494 ms 4584 KB correct
94 Correct 349 ms 3104 KB correct
95 Correct 325 ms 3272 KB correct
96 Correct 180 ms 1816 KB correct
97 Correct 210 ms 1984 KB correct
98 Correct 243 ms 2624 KB correct
99 Correct 218 ms 2020 KB correct
100 Correct 77 ms 912 KB correct
101 Correct 30 ms 504 KB correct
102 Correct 525 ms 4180 KB correct
103 Correct 505 ms 4216 KB correct
104 Correct 492 ms 4068 KB correct
105 Correct 504 ms 4280 KB correct