답안 #793486

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
793486 2023-07-25T22:36:05 Z farhan132 Simurgh (IOI17_simurgh) C++17
100 / 100
188 ms 8152 KB
#include "simurgh.h"
#include <bits/stdc++.h>
 
using namespace std;
 
typedef int ll;
typedef double dd;
typedef pair<ll , ll> ii;
typedef tuple < ll,  ll, ll > tp;
 
#define ff first
#define ss second
#define pb push_back
#define in insert
#define mem(a , b) memset(a, b ,sizeof(a))

const ll N = 505;  
vector < ii > v[N];
vector < tuple < ll , ll, ll > > e, Nice;
ll n, col[N * N], par[N], vis[N], edge[N], deg[N], D[N];

void dfs(ll node, ll shit){
	vis[node] = 1;
	for(auto [u, i] : v[node]){
		if(!vis[u]){
			edge[u] = i;
			D[u] = D[node] + 1; 
			par[u] = node;
			Nice.pb({node, u, i});
			dfs(u, 1);
		}
	}
}

ll Norm = 0;

void dfs(ll node){
	vis[node] = 1;
	for(auto [u, i] : v[node]){
		if(!vis[u]){
			dfs(u);
		}else{
			if(D[u] > D[node] || D[u] + 1 == D[node]) continue;
			vector < ll > edges, extra;
			ll cur = node;
			while(cur != u){
				if(col[edge[cur]] == -1) edges.pb(edge[cur]);
				else extra.pb(edge[cur]);
				cur = par[cur];
			}
			if(edges.size()){
				vector < ii > val; val.pb({Norm, i});
				if(extra.size()){
					edges.pb(extra[0]);
				}
				for(auto x : edges){
					vector < ll > query;
					for(auto [_ , __, j] : Nice){
						if(j == x) query.pb(i);
						else query.pb(j);
					}
					val.pb({count_common_roads(query), x});
				}
				sort(val.begin(), val.end());
				if(val[0].ff == val.back().ff){
					if(extra.size()){
						for(auto [_, x] : val) col[x] = col[extra[0]];
					}else{
						for(auto [_, x] : val) col[x] = 0;
					}
					
				}else{
					for(auto [_ , x] : val){
						col[x] = (_ == val[0].ff);
					}
				}
			}
		}
	}
	return;
}

struct DSU{
    vector < int > par;
    void start(int n){
        par.resize(n + 1);
        for(int i = 0; i <= n; i++) par[i] = i;
    }
    int find(int a){
        if(a == par[a]) return a;
        return par[a] = find(par[a]);
    }
    void merge(int x, int y){
        x = find(x); y = find(y);
        if(x == y) return;
        par[x] = y;
        return;
    }
};

ll plag(vector < tuple < ll , ll, ll > > E){
	DSU T; T.start(n);
	vector < ll > golden;
	for(auto [x, y, i] : E){
		T.merge(x, y);
		golden.pb(i);
	}
	ll S = 0;
	for(auto [x, y, i] : Nice){
		if(T.find(x) == T.find(y)) continue;
		T.merge(x, y);
		golden.pb(i);
		S += col[i];
	}
	return count_common_roads(golden) - S;
}


std::vector<int> find_roads(int _n, std::vector<int> _u, std::vector<int> _v) {
	n = _n;
	for(ll i = 0; i < _u.size(); i++){
		e.pb({_u[i], _v[i], i});
	}
	for(auto [x, y, i] : e){
		v[x].pb({y, i});
		v[y].pb({x, i});
	}
	mem(col, -1);
	mem(vis, 0);

	par[0] = -1; 
	D[0] = 0;
	dfs(0, 1);

	vector < ll > golden;
	for(auto [x, y, i] : Nice){
		golden.pb(i);
	}
	Norm = count_common_roads(golden);
	mem(vis, 0);
	dfs(0);
	ll Sum = 0;

	for(auto [x, y, i] : Nice){
		if(col[i] == -1){
			col[i] = 1;
		}
		Sum += col[i];
	}
	assert(Norm == Sum);

	queue < ll > q;

	for(ll i = 0; i < n; i++){
		vector < tuple < ll , ll , ll > > dump;
		for(auto [u, j] : v[i]){
			dump.pb({i, u, j});
		}
		deg[i] = plag(dump);
		if(deg[i] == 1) q.push(i);
	}
	vector < ll > royal;
	mem(vis, 0);

	for(ll itr = 0; itr < n - 1; itr++){
		ll node = q.front(); q.pop();
		vis[node] = 1;
		vector < tuple < ll , ll , ll > > E;
		for(auto [u, i] : v[node]){
			if(vis[u] == 1) continue; 
			E.push_back({node, u, i});
		}
		ll l = 0, r = E.size() - 1, k = 0;
		while(l <= r){
			ll m = (l + r) / 2;
			vector < tuple < ll , ll , ll > > dump;
			for(ll i = 0; i <= m; i++) dump.pb(E[i]);
			if(plag(dump)){
				k = m;
				r = m - 1;
			}else{
				l = m + 1;
			}
		}
		auto [_, nxt, i] = E[k];
		royal.pb(i);
		deg[nxt]--;
		//cout << node << ' ' << nxt << '\n';
		if(deg[nxt] == 1) q.push(nxt);
	}

	return royal;
}

Compilation message

simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:121:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |  for(ll i = 0; i < _u.size(); i++){
      |                ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1236 KB correct
2 Correct 1 ms 1236 KB correct
3 Correct 1 ms 1236 KB correct
4 Correct 1 ms 1220 KB correct
5 Correct 1 ms 1236 KB correct
6 Correct 1 ms 1236 KB correct
7 Correct 1 ms 1236 KB correct
8 Correct 1 ms 1216 KB correct
9 Correct 1 ms 1236 KB correct
10 Correct 1 ms 1236 KB correct
11 Correct 1 ms 1236 KB correct
12 Correct 1 ms 1236 KB correct
13 Correct 1 ms 1236 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1236 KB correct
2 Correct 1 ms 1236 KB correct
3 Correct 1 ms 1236 KB correct
4 Correct 1 ms 1220 KB correct
5 Correct 1 ms 1236 KB correct
6 Correct 1 ms 1236 KB correct
7 Correct 1 ms 1236 KB correct
8 Correct 1 ms 1216 KB correct
9 Correct 1 ms 1236 KB correct
10 Correct 1 ms 1236 KB correct
11 Correct 1 ms 1236 KB correct
12 Correct 1 ms 1236 KB correct
13 Correct 1 ms 1236 KB correct
14 Correct 2 ms 1364 KB correct
15 Correct 2 ms 1364 KB correct
16 Correct 2 ms 1364 KB correct
17 Correct 2 ms 1364 KB correct
18 Correct 1 ms 1364 KB correct
19 Correct 2 ms 1364 KB correct
20 Correct 2 ms 1364 KB correct
21 Correct 2 ms 1364 KB correct
22 Correct 1 ms 1364 KB correct
23 Correct 1 ms 1364 KB correct
24 Correct 1 ms 1364 KB correct
25 Correct 1 ms 1328 KB correct
26 Correct 1 ms 1364 KB correct
27 Correct 1 ms 1364 KB correct
28 Correct 1 ms 1236 KB correct
29 Correct 1 ms 1236 KB correct
30 Correct 1 ms 1364 KB correct
31 Correct 1 ms 1364 KB correct
32 Correct 1 ms 1364 KB correct
33 Correct 1 ms 1364 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1236 KB correct
2 Correct 1 ms 1236 KB correct
3 Correct 1 ms 1236 KB correct
4 Correct 1 ms 1220 KB correct
5 Correct 1 ms 1236 KB correct
6 Correct 1 ms 1236 KB correct
7 Correct 1 ms 1236 KB correct
8 Correct 1 ms 1216 KB correct
9 Correct 1 ms 1236 KB correct
10 Correct 1 ms 1236 KB correct
11 Correct 1 ms 1236 KB correct
12 Correct 1 ms 1236 KB correct
13 Correct 1 ms 1236 KB correct
14 Correct 2 ms 1364 KB correct
15 Correct 2 ms 1364 KB correct
16 Correct 2 ms 1364 KB correct
17 Correct 2 ms 1364 KB correct
18 Correct 1 ms 1364 KB correct
19 Correct 2 ms 1364 KB correct
20 Correct 2 ms 1364 KB correct
21 Correct 2 ms 1364 KB correct
22 Correct 1 ms 1364 KB correct
23 Correct 1 ms 1364 KB correct
24 Correct 1 ms 1364 KB correct
25 Correct 1 ms 1328 KB correct
26 Correct 1 ms 1364 KB correct
27 Correct 1 ms 1364 KB correct
28 Correct 1 ms 1236 KB correct
29 Correct 1 ms 1236 KB correct
30 Correct 1 ms 1364 KB correct
31 Correct 1 ms 1364 KB correct
32 Correct 1 ms 1364 KB correct
33 Correct 1 ms 1364 KB correct
34 Correct 30 ms 2896 KB correct
35 Correct 33 ms 2888 KB correct
36 Correct 23 ms 2632 KB correct
37 Correct 6 ms 1356 KB correct
38 Correct 29 ms 2900 KB correct
39 Correct 26 ms 2816 KB correct
40 Correct 28 ms 2640 KB correct
41 Correct 29 ms 2896 KB correct
42 Correct 29 ms 2916 KB correct
43 Correct 15 ms 2324 KB correct
44 Correct 14 ms 2064 KB correct
45 Correct 15 ms 2072 KB correct
46 Correct 13 ms 1940 KB correct
47 Correct 8 ms 1612 KB correct
48 Correct 4 ms 1364 KB correct
49 Correct 5 ms 1480 KB correct
50 Correct 8 ms 1612 KB correct
51 Correct 15 ms 2072 KB correct
52 Correct 13 ms 2068 KB correct
53 Correct 12 ms 1944 KB correct
54 Correct 16 ms 2324 KB correct
55 Correct 17 ms 2196 KB correct
56 Correct 17 ms 2196 KB correct
57 Correct 17 ms 2196 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1244 KB correct
2 Correct 1 ms 1240 KB correct
3 Correct 94 ms 6060 KB correct
4 Correct 165 ms 8124 KB correct
5 Correct 159 ms 8088 KB correct
6 Correct 161 ms 8152 KB correct
7 Correct 159 ms 8132 KB correct
8 Correct 166 ms 8104 KB correct
9 Correct 157 ms 8124 KB correct
10 Correct 159 ms 8132 KB correct
11 Correct 159 ms 8088 KB correct
12 Correct 188 ms 8056 KB correct
13 Correct 1 ms 1236 KB correct
14 Correct 158 ms 8092 KB correct
15 Correct 158 ms 8096 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1236 KB correct
2 Correct 1 ms 1236 KB correct
3 Correct 1 ms 1236 KB correct
4 Correct 1 ms 1220 KB correct
5 Correct 1 ms 1236 KB correct
6 Correct 1 ms 1236 KB correct
7 Correct 1 ms 1236 KB correct
8 Correct 1 ms 1216 KB correct
9 Correct 1 ms 1236 KB correct
10 Correct 1 ms 1236 KB correct
11 Correct 1 ms 1236 KB correct
12 Correct 1 ms 1236 KB correct
13 Correct 1 ms 1236 KB correct
14 Correct 2 ms 1364 KB correct
15 Correct 2 ms 1364 KB correct
16 Correct 2 ms 1364 KB correct
17 Correct 2 ms 1364 KB correct
18 Correct 1 ms 1364 KB correct
19 Correct 2 ms 1364 KB correct
20 Correct 2 ms 1364 KB correct
21 Correct 2 ms 1364 KB correct
22 Correct 1 ms 1364 KB correct
23 Correct 1 ms 1364 KB correct
24 Correct 1 ms 1364 KB correct
25 Correct 1 ms 1328 KB correct
26 Correct 1 ms 1364 KB correct
27 Correct 1 ms 1364 KB correct
28 Correct 1 ms 1236 KB correct
29 Correct 1 ms 1236 KB correct
30 Correct 1 ms 1364 KB correct
31 Correct 1 ms 1364 KB correct
32 Correct 1 ms 1364 KB correct
33 Correct 1 ms 1364 KB correct
34 Correct 30 ms 2896 KB correct
35 Correct 33 ms 2888 KB correct
36 Correct 23 ms 2632 KB correct
37 Correct 6 ms 1356 KB correct
38 Correct 29 ms 2900 KB correct
39 Correct 26 ms 2816 KB correct
40 Correct 28 ms 2640 KB correct
41 Correct 29 ms 2896 KB correct
42 Correct 29 ms 2916 KB correct
43 Correct 15 ms 2324 KB correct
44 Correct 14 ms 2064 KB correct
45 Correct 15 ms 2072 KB correct
46 Correct 13 ms 1940 KB correct
47 Correct 8 ms 1612 KB correct
48 Correct 4 ms 1364 KB correct
49 Correct 5 ms 1480 KB correct
50 Correct 8 ms 1612 KB correct
51 Correct 15 ms 2072 KB correct
52 Correct 13 ms 2068 KB correct
53 Correct 12 ms 1944 KB correct
54 Correct 16 ms 2324 KB correct
55 Correct 17 ms 2196 KB correct
56 Correct 17 ms 2196 KB correct
57 Correct 17 ms 2196 KB correct
58 Correct 1 ms 1244 KB correct
59 Correct 1 ms 1240 KB correct
60 Correct 94 ms 6060 KB correct
61 Correct 165 ms 8124 KB correct
62 Correct 159 ms 8088 KB correct
63 Correct 161 ms 8152 KB correct
64 Correct 159 ms 8132 KB correct
65 Correct 166 ms 8104 KB correct
66 Correct 157 ms 8124 KB correct
67 Correct 159 ms 8132 KB correct
68 Correct 159 ms 8088 KB correct
69 Correct 188 ms 8056 KB correct
70 Correct 1 ms 1236 KB correct
71 Correct 158 ms 8092 KB correct
72 Correct 158 ms 8096 KB correct
73 Correct 1 ms 1236 KB correct
74 Correct 161 ms 8100 KB correct
75 Correct 160 ms 8052 KB correct
76 Correct 75 ms 3912 KB correct
77 Correct 164 ms 8088 KB correct
78 Correct 163 ms 8108 KB correct
79 Correct 161 ms 8044 KB correct
80 Correct 155 ms 7996 KB correct
81 Correct 140 ms 7248 KB correct
82 Correct 158 ms 7972 KB correct
83 Correct 101 ms 5092 KB correct
84 Correct 91 ms 5808 KB correct
85 Correct 74 ms 5444 KB correct
86 Correct 56 ms 4072 KB correct
87 Correct 44 ms 3476 KB correct
88 Correct 40 ms 2768 KB correct
89 Correct 40 ms 2776 KB correct
90 Correct 37 ms 2640 KB correct
91 Correct 19 ms 1544 KB correct
92 Correct 14 ms 1364 KB correct
93 Correct 73 ms 5548 KB correct
94 Correct 56 ms 4072 KB correct
95 Correct 65 ms 4168 KB correct
96 Correct 36 ms 2748 KB correct
97 Correct 39 ms 2888 KB correct
98 Correct 47 ms 3536 KB correct
99 Correct 49 ms 2836 KB correct
100 Correct 24 ms 1748 KB correct
101 Correct 15 ms 1516 KB correct
102 Correct 88 ms 4816 KB correct
103 Correct 90 ms 4820 KB correct
104 Correct 88 ms 4672 KB correct
105 Correct 87 ms 4784 KB correct