Submission #135033

# Submission time Handle Problem Language Result Execution time Memory
135033 2019-07-23T14:41:20 Z tmwilliamlin168 Simurgh (IOI17_simurgh) C++14
100 / 100
956 ms 8204 KB
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;

#define vi vector<int>
#define pb push_back
#define ccr count_common_roads

const int mN=500, mM=124750;
int n, tin[mN], low[mN], dt=1, b[mM], bs, st[mM], sth, t[mN], p[mN], c[mN];
vi eu, ev, adj[mN], ans;

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(vi w, vi &r) {
	for(int i=0; i<eu.size(); ++i)
		if(w[i]&&join(eu[i], ev[i]))
			r.pb(i);
}

void dc(vi &a, vi &b, vi &c, int l, int r, int s1, int s2) {
	if(s2==s1)
		return;
	if(l==r) {
		c.pb(b[l]);
		return;
	}
	int m=(l+r)/2;
	vi d=a;
	for(int i=l; i<=m; ++i)
		d[i]=b[i];
	int sl=ccr(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) {
	vi v1, v2, r;
	for(int e : adj[u]) {
		int v=eu[e]^ev[e]^u;
		if(c[v])
			continue;
		if(t[v]<0) {
			v2.pb(e);
			t[v]=e;
		} else
			v1.pb(e);
	}
	if(v1.size()) {
		iota(p, p+n, 0);
		vi s1, w(eu.size(), 1), s2=v1;
		for(int i=0; i<v1.size(); ++i) {
			s1.pb(t[eu[v1[i]]^ev[v1[i]]^u]);
			join(eu[s1.back()], ev[s1.back()]);
		}
		for(int e : ans) {
			s1.pb(e);
			join(eu[e], ev[e]);
		}
		for(int e : v1) {
			w[e]=0;
			w[t[eu[e]^ev[e]^u]]=0;
		}
		fst(w, s1);
		s2.insert(s2.end(), s1.begin()+v1.size(), s1.end());
		dc(s1, v1, r, 0, v1.size()-1, ccr(s1), ccr(s2));
	}
	if(v2.size()) {
		vi w(eu.size(), 1), s;
		for(int e : adj[u])
			w[e]=0;
		iota(p, p+n, 0);
		fst(w, s);
		for(int e : r)
			if(join(eu[e], ev[e]))
				s.pb(e);
		if(~pe&&join(eu[pe], ev[pe]))
			s.pb(pe);
		for(int e : adj[u])
			if(join(eu[e], ev[e]))
				s.pb(e);
		int bc=ccr(s);
		sort(v2.begin(), v2.end(), [](const int &i, const int &j) {
			return b[i]<b[j];
		});
		for(int i=0, j=0, o; i<v2.size(); ) {
			for(; j<v2.size()&&b[v2[j]]==b[v2[i]]; ++j);
			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));
			vi a;
			int cm=bc;
			for(; i<j; ++i) {
				s.pb(v2[i]);
				int cc=ccr(s);
				if(cc>cm) {
					cm=cc;
					a=vector<int>();
				}
				if(cc==cm)
					a.pb(v2[i]);
				s.pop_back();
			}
			s.pb(o);
			r.insert(r.end(), a.begin(), a.end());
		}
	}
	for(int e : r) {
		ans.pb(e);
		c[eu[e]^ev[e]^u]=1;
	}
	for(int e : r)
		dfs2(eu[e]^ev[e]^u, e);
}

vi find_roads(int n, vi u, vi v) {
	::n=n;
	eu=u;
	ev=v;
	for(int i=0; i<u.size(); ++i) {
		adj[u[i]].pb(i);
		adj[v[i]].pb(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:46: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:82:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0; i<v1.size(); ++i) {
                ~^~~~~~~~~~
simurgh.cpp:116:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0, j=0, o; i<v2.size(); ) {
                        ~^~~~~~~~~~
simurgh.cpp:117: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:151:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<u.size(); ++i) {
               ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 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 380 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 380 KB correct
13 Correct 2 ms 376 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 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 380 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 380 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 4 ms 504 KB correct
15 Correct 3 ms 376 KB correct
16 Correct 3 ms 504 KB correct
17 Correct 4 ms 376 KB correct
18 Correct 3 ms 376 KB correct
19 Correct 3 ms 376 KB correct
20 Correct 3 ms 376 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 3 ms 376 KB correct
30 Correct 3 ms 504 KB correct
31 Correct 3 ms 376 KB correct
32 Correct 3 ms 376 KB correct
33 Correct 3 ms 376 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 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 380 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 380 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 4 ms 504 KB correct
15 Correct 3 ms 376 KB correct
16 Correct 3 ms 504 KB correct
17 Correct 4 ms 376 KB correct
18 Correct 3 ms 376 KB correct
19 Correct 3 ms 376 KB correct
20 Correct 3 ms 376 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 3 ms 376 KB correct
30 Correct 3 ms 504 KB correct
31 Correct 3 ms 376 KB correct
32 Correct 3 ms 376 KB correct
33 Correct 3 ms 376 KB correct
34 Correct 107 ms 2132 KB correct
35 Correct 105 ms 2108 KB correct
36 Correct 75 ms 1552 KB correct
37 Correct 19 ms 376 KB correct
38 Correct 107 ms 2152 KB correct
39 Correct 86 ms 1792 KB correct
40 Correct 66 ms 1680 KB correct
41 Correct 15 ms 2136 KB correct
42 Correct 14 ms 2040 KB correct
43 Correct 52 ms 1272 KB correct
44 Correct 45 ms 1092 KB correct
45 Correct 50 ms 1144 KB correct
46 Correct 42 ms 1144 KB correct
47 Correct 24 ms 632 KB correct
48 Correct 8 ms 376 KB correct
49 Correct 14 ms 504 KB correct
50 Correct 25 ms 632 KB correct
51 Correct 52 ms 1152 KB correct
52 Correct 46 ms 1144 KB correct
53 Correct 42 ms 1016 KB correct
54 Correct 56 ms 1380 KB correct
55 Correct 53 ms 1148 KB correct
56 Correct 54 ms 1144 KB correct
57 Correct 54 ms 1144 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 485 ms 5072 KB correct
4 Correct 938 ms 7516 KB correct
5 Correct 939 ms 7616 KB correct
6 Correct 57 ms 7332 KB correct
7 Correct 236 ms 7460 KB correct
8 Correct 280 ms 7436 KB correct
9 Correct 931 ms 7548 KB correct
10 Correct 956 ms 8072 KB correct
11 Correct 932 ms 7672 KB correct
12 Correct 935 ms 8028 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 541 ms 7432 KB correct
15 Correct 950 ms 8204 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 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 380 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 380 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 4 ms 504 KB correct
15 Correct 3 ms 376 KB correct
16 Correct 3 ms 504 KB correct
17 Correct 4 ms 376 KB correct
18 Correct 3 ms 376 KB correct
19 Correct 3 ms 376 KB correct
20 Correct 3 ms 376 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 3 ms 376 KB correct
30 Correct 3 ms 504 KB correct
31 Correct 3 ms 376 KB correct
32 Correct 3 ms 376 KB correct
33 Correct 3 ms 376 KB correct
34 Correct 107 ms 2132 KB correct
35 Correct 105 ms 2108 KB correct
36 Correct 75 ms 1552 KB correct
37 Correct 19 ms 376 KB correct
38 Correct 107 ms 2152 KB correct
39 Correct 86 ms 1792 KB correct
40 Correct 66 ms 1680 KB correct
41 Correct 15 ms 2136 KB correct
42 Correct 14 ms 2040 KB correct
43 Correct 52 ms 1272 KB correct
44 Correct 45 ms 1092 KB correct
45 Correct 50 ms 1144 KB correct
46 Correct 42 ms 1144 KB correct
47 Correct 24 ms 632 KB correct
48 Correct 8 ms 376 KB correct
49 Correct 14 ms 504 KB correct
50 Correct 25 ms 632 KB correct
51 Correct 52 ms 1152 KB correct
52 Correct 46 ms 1144 KB correct
53 Correct 42 ms 1016 KB correct
54 Correct 56 ms 1380 KB correct
55 Correct 53 ms 1148 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 485 ms 5072 KB correct
61 Correct 938 ms 7516 KB correct
62 Correct 939 ms 7616 KB correct
63 Correct 57 ms 7332 KB correct
64 Correct 236 ms 7460 KB correct
65 Correct 280 ms 7436 KB correct
66 Correct 931 ms 7548 KB correct
67 Correct 956 ms 8072 KB correct
68 Correct 932 ms 7672 KB correct
69 Correct 935 ms 8028 KB correct
70 Correct 2 ms 376 KB correct
71 Correct 541 ms 7432 KB correct
72 Correct 950 ms 8204 KB correct
73 Correct 2 ms 376 KB correct
74 Correct 940 ms 7536 KB correct
75 Correct 938 ms 7408 KB correct
76 Correct 300 ms 2972 KB correct
77 Correct 943 ms 7628 KB correct
78 Correct 954 ms 7576 KB correct
79 Correct 937 ms 7584 KB correct
80 Correct 920 ms 7500 KB correct
81 Correct 49 ms 6268 KB correct
82 Correct 904 ms 8124 KB correct
83 Correct 518 ms 4136 KB correct
84 Correct 541 ms 4840 KB correct
85 Correct 500 ms 4480 KB correct
86 Correct 359 ms 3084 KB correct
87 Correct 251 ms 2456 KB correct
88 Correct 205 ms 1848 KB correct
89 Correct 190 ms 1840 KB correct
90 Correct 166 ms 1872 KB correct
91 Correct 53 ms 504 KB correct
92 Correct 30 ms 380 KB correct
93 Correct 503 ms 4476 KB correct
94 Correct 350 ms 3072 KB correct
95 Correct 333 ms 3052 KB correct
96 Correct 181 ms 1848 KB correct
97 Correct 211 ms 1952 KB correct
98 Correct 249 ms 2632 KB correct
99 Correct 220 ms 2104 KB correct
100 Correct 76 ms 760 KB correct
101 Correct 29 ms 524 KB correct
102 Correct 493 ms 4004 KB correct
103 Correct 498 ms 4056 KB correct
104 Correct 499 ms 4248 KB correct
105 Correct 486 ms 4020 KB correct