# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
147013 | 2019-08-27T05:40:15 Z | gs14004 | City Mapping (NOI18_citymapping) | C++17 | 36 ms | 8516 KB |
#include "citymapping.h" #include <bits/stdc++.h> #define sz(v) ((int)(v).size()) using namespace std; using lint = long long; using pi = pair<lint, lint>; const int MAXN = 1005; lint mem[MAXN][MAXN]; lint dist(int x, int y){ if(x == y) return 0; if(mem[x][y]) return mem[x][y]; return mem[x][y] = mem[y][x] = get_distance(x, y); } vector<pi> gph[MAXN]; lint dep[MAXN]; bool vis[MAXN]; vector<int> dfn; int sz[MAXN], msz[MAXN], par[MAXN]; void dfs(int x, int p){ sz[x] = 1; msz[x] = 0; for(auto &i : gph[x]){ if(!vis[i.second] && i.second != p){ dfs(i.second, x); sz[x] += sz[i.second]; msz[x] = max(msz[x], sz[i.second]); } } dfn.push_back(x); } int get_center(int v){ dfn.clear(); dfs(v, -1); pi ret(1e9, -1); for(auto &i : dfn){ pi ans = pi(max(sz(dfn) - sz[i], msz[i]), i); ret = min(ret, ans); } return ret.second; } void find_roads(int N, int Q, int A[], int B[], int W[]) { auto make_edge = [&](int s, int e, int x){ // printf("make_edge(%d, %d, %d)\n", s, e, x); gph[s].emplace_back(x, e); gph[e].emplace_back(x, s); for(int i=0; i<N-1; i++){ if(!W[i]){ tie(A[i], B[i], W[i]) = make_tuple(s, e, x); break; } } }; vector<pi> v; for(int i=2; i<=N; i++){ dep[i] = dist(1, i); v.emplace_back(dep[i], i); } sort(v.begin(), v.end()); vector<int> prv = {1}; for(auto &i : v){ if(sz(prv) == 1){ par[i.second] = 1; make_edge(1, i.second, i.first); prv.push_back(i.second); continue; } memset(vis, 0, sizeof(vis)); int cur_check = 1; pi lowest(dep[1], 1); while(cur_check && !vis[cur_check]){ int c = get_center(cur_check); vis[c] = 1; vector<int> sons; for(auto &j : gph[c]){ if(!vis[j.second] && dep[j.second] > dep[c]){ sons.push_back(j.second); } } if(sons.empty()){ if(dist(1, c) + dist(c, i.second) == dist(1, i.second)){ lowest = max(lowest, pi(dep[c], c)); break; } else{ lint branch = (dist(1, c) + dist(c, i.second) - dist(1, i.second)) / 2; lint d1 = dep[c]; c = par[c]; while(d1 - branch < dep[c]){ if(vis[c]) break; vis[c] = 1; cur_check = par[c]; } } continue; } int next_trial = sons[0]; lint d1 = dep[next_trial]; lint d2 = dist(next_trial, i.second); lint d3 = dep[i.second]; if(d1 + d2 == d3){ lowest = max(lowest, pi(dep[next_trial], next_trial)); cur_check = next_trial; continue; } lint branch = (d1 + d2 - d3) / 2; if(d1 - branch < dep[c]){ cur_check = par[c]; while(d1 - branch < dep[cur_check]){ if(vis[cur_check]) break; vis[cur_check] = 1; cur_check = par[cur_check]; } } else{ lowest = max(lowest, pi(dep[c], c)); if(sz(sons) >= 2){ cur_check = sons[1]; } if(sz(sons) == 3){ if(dist(1, sons[2]) + dist(sons[2], i.second) == dist(1, i.second)){ cur_check = sons[2]; } } } } make_edge(lowest.second, i.second, dist(lowest.second, i.second)); par[i.second] = lowest.second; prv.push_back(i.second); } }
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 33 ms | 8440 KB | Correct: 8705 out of 500000 queries used. |
2 | Correct | 32 ms | 8440 KB | Correct: 9050 out of 500000 queries used. |
3 | Correct | 27 ms | 8440 KB | Correct: 7306 out of 500000 queries used. |
4 | Correct | 26 ms | 8440 KB | Correct: 7723 out of 500000 queries used. |
5 | Correct | 30 ms | 8440 KB | Correct: 7432 out of 500000 queries used. |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 33 ms | 8440 KB | Correct: 8705 out of 500000 queries used. |
2 | Correct | 32 ms | 8440 KB | Correct: 9050 out of 500000 queries used. |
3 | Correct | 27 ms | 8440 KB | Correct: 7306 out of 500000 queries used. |
4 | Correct | 26 ms | 8440 KB | Correct: 7723 out of 500000 queries used. |
5 | Correct | 30 ms | 8440 KB | Correct: 7432 out of 500000 queries used. |
6 | Correct | 32 ms | 8440 KB | Correct: 8688 out of 500000 queries used. |
7 | Correct | 31 ms | 8440 KB | Correct: 7958 out of 500000 queries used. |
8 | Correct | 28 ms | 8440 KB | Correct: 7147 out of 500000 queries used. |
9 | Correct | 26 ms | 8440 KB | Correct: 7246 out of 500000 queries used. |
10 | Correct | 30 ms | 8440 KB | Correct: 7393 out of 500000 queries used. |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 31 ms | 8440 KB | Correct: 8635 out of 12000 queries used. |
2 | Correct | 31 ms | 8440 KB | Correct: 8652 out of 12000 queries used. |
3 | Correct | 33 ms | 8440 KB | Correct: 8736 out of 12000 queries used. |
4 | Correct | 33 ms | 8412 KB | Correct: 8646 out of 12000 queries used. |
5 | Correct | 32 ms | 8440 KB | Correct: 8641 out of 12000 queries used. |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 31 ms | 8440 KB | Correct: 8635 out of 12000 queries used. |
2 | Correct | 31 ms | 8440 KB | Correct: 8652 out of 12000 queries used. |
3 | Correct | 33 ms | 8440 KB | Correct: 8736 out of 12000 queries used. |
4 | Correct | 33 ms | 8412 KB | Correct: 8646 out of 12000 queries used. |
5 | Correct | 32 ms | 8440 KB | Correct: 8641 out of 12000 queries used. |
6 | Correct | 33 ms | 8452 KB | Correct: 8696 out of 12000 queries used. |
7 | Correct | 33 ms | 8440 KB | Correct: 8653 out of 12000 queries used. |
8 | Correct | 33 ms | 8456 KB | Correct: 8721 out of 12000 queries used. |
9 | Correct | 32 ms | 8440 KB | Correct: 8696 out of 12000 queries used. |
10 | Correct | 34 ms | 8440 KB | Correct: 8652 out of 12000 queries used. |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 33 ms | 8440 KB | Correct: 8705 out of 500000 queries used. |
2 | Correct | 32 ms | 8440 KB | Correct: 9050 out of 500000 queries used. |
3 | Correct | 27 ms | 8440 KB | Correct: 7306 out of 500000 queries used. |
4 | Correct | 26 ms | 8440 KB | Correct: 7723 out of 500000 queries used. |
5 | Correct | 30 ms | 8440 KB | Correct: 7432 out of 500000 queries used. |
6 | Correct | 32 ms | 8440 KB | Correct: 8688 out of 500000 queries used. |
7 | Correct | 31 ms | 8440 KB | Correct: 7958 out of 500000 queries used. |
8 | Correct | 28 ms | 8440 KB | Correct: 7147 out of 500000 queries used. |
9 | Correct | 26 ms | 8440 KB | Correct: 7246 out of 500000 queries used. |
10 | Correct | 30 ms | 8440 KB | Correct: 7393 out of 500000 queries used. |
11 | Correct | 31 ms | 8440 KB | Correct: 8635 out of 12000 queries used. |
12 | Correct | 31 ms | 8440 KB | Correct: 8652 out of 12000 queries used. |
13 | Correct | 33 ms | 8440 KB | Correct: 8736 out of 12000 queries used. |
14 | Correct | 33 ms | 8412 KB | Correct: 8646 out of 12000 queries used. |
15 | Correct | 32 ms | 8440 KB | Correct: 8641 out of 12000 queries used. |
16 | Correct | 33 ms | 8452 KB | Correct: 8696 out of 12000 queries used. |
17 | Correct | 33 ms | 8440 KB | Correct: 8653 out of 12000 queries used. |
18 | Correct | 33 ms | 8456 KB | Correct: 8721 out of 12000 queries used. |
19 | Correct | 32 ms | 8440 KB | Correct: 8696 out of 12000 queries used. |
20 | Correct | 34 ms | 8440 KB | Correct: 8652 out of 12000 queries used. |
21 | Correct | 36 ms | 8440 KB | Correct: 8674 out of 25000 queries used. |
22 | Correct | 36 ms | 8440 KB | Correct: 7992 out of 25000 queries used. |
23 | Correct | 30 ms | 8368 KB | Correct: 7808 out of 25000 queries used. |
24 | Correct | 30 ms | 8440 KB | Correct: 7812 out of 25000 queries used. |
25 | Correct | 28 ms | 8440 KB | Correct: 7225 out of 25000 queries used. |
26 | Correct | 27 ms | 8440 KB | Correct: 7221 out of 25000 queries used. |
27 | Correct | 27 ms | 8440 KB | Correct: 7185 out of 25000 queries used. |
28 | Correct | 27 ms | 8440 KB | Correct: 7149 out of 25000 queries used. |
29 | Correct | 28 ms | 8440 KB | Correct: 7064 out of 25000 queries used. |
30 | Correct | 27 ms | 8440 KB | Correct: 7186 out of 25000 queries used. |
31 | Correct | 27 ms | 8412 KB | Correct: 7345 out of 25000 queries used. |
32 | Correct | 32 ms | 8440 KB | Correct: 8649 out of 25000 queries used. |
33 | Correct | 29 ms | 8440 KB | Correct: 7228 out of 25000 queries used. |
34 | Correct | 27 ms | 8516 KB | Correct: 7327 out of 25000 queries used. |
35 | Correct | 30 ms | 8440 KB | Correct: 7238 out of 25000 queries used. |
36 | Correct | 28 ms | 8412 KB | Correct: 7262 out of 25000 queries used. |
37 | Correct | 26 ms | 8440 KB | Correct: 7296 out of 25000 queries used. |
38 | Correct | 27 ms | 8440 KB | Correct: 7211 out of 25000 queries used. |
39 | Correct | 27 ms | 8412 KB | Correct: 7310 out of 25000 queries used. |
40 | Correct | 29 ms | 8440 KB | Correct: 7216 out of 25000 queries used. |
41 | Correct | 27 ms | 8440 KB | Correct: 7187 out of 25000 queries used. |
42 | Correct | 26 ms | 8440 KB | Correct: 7254 out of 25000 queries used. |
43 | Correct | 32 ms | 8440 KB | Correct: 8688 out of 25000 queries used. |
44 | Correct | 26 ms | 8312 KB | Correct: 7227 out of 25000 queries used. |
45 | Correct | 26 ms | 8444 KB | Correct: 7211 out of 25000 queries used. |
46 | Correct | 26 ms | 8312 KB | Correct: 7161 out of 25000 queries used. |
47 | Correct | 27 ms | 8440 KB | Correct: 7258 out of 25000 queries used. |
48 | Correct | 26 ms | 8440 KB | Correct: 7200 out of 25000 queries used. |
49 | Correct | 29 ms | 8440 KB | Correct: 7264 out of 25000 queries used. |
50 | Correct | 30 ms | 8440 KB | Correct: 7210 out of 25000 queries used. |
51 | Correct | 27 ms | 8440 KB | Correct: 7224 out of 25000 queries used. |
52 | Correct | 36 ms | 8492 KB | Correct: 7276 out of 25000 queries used. |
53 | Correct | 27 ms | 8440 KB | Correct: 7268 out of 25000 queries used. |
54 | Correct | 32 ms | 8440 KB | Correct: 7842 out of 25000 queries used. |
55 | Correct | 29 ms | 8440 KB | Correct: 7252 out of 25000 queries used. |
56 | Correct | 27 ms | 8440 KB | Correct: 7242 out of 25000 queries used. |
57 | Correct | 30 ms | 8440 KB | Correct: 7288 out of 25000 queries used. |
58 | Correct | 26 ms | 8316 KB | Correct: 7231 out of 25000 queries used. |
59 | Correct | 26 ms | 8444 KB | Correct: 7226 out of 25000 queries used. |
60 | Correct | 29 ms | 8444 KB | Correct: 7359 out of 25000 queries used. |
61 | Correct | 30 ms | 8440 KB | Correct: 7348 out of 25000 queries used. |
62 | Correct | 29 ms | 8440 KB | Correct: 7316 out of 25000 queries used. |
63 | Correct | 30 ms | 8428 KB | Correct: 7377 out of 25000 queries used. |
64 | Correct | 29 ms | 8440 KB | Correct: 7287 out of 25000 queries used. |
65 | Correct | 29 ms | 8440 KB | Correct: 7681 out of 25000 queries used. |
66 | Correct | 30 ms | 8440 KB | Correct: 7378 out of 25000 queries used. |
67 | Correct | 30 ms | 8444 KB | Correct: 7973 out of 25000 queries used. |
68 | Correct | 32 ms | 8440 KB | Correct: 7729 out of 25000 queries used. |
69 | Correct | 31 ms | 8444 KB | Correct: 7784 out of 25000 queries used. |
70 | Correct | 30 ms | 8444 KB | Correct: 7749 out of 25000 queries used. |