Submission #1694903
Source Code Expand
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <cmath> using namespace std; const int MAXN = 200000 + 1000; int n, m; long long ans = 0; vector<int> edg[MAXN]; bool iss[MAXN]; vector<int> spe; int fa[MAXN], dup[MAXN], ddo[MAXN], cnt[MAXN]; void dp1(int x, int father) { ddo[x] = 1, cnt[x] = iss[x], fa[x] = father; for(int i = 0; i < edg[x].size(); i++) if(edg[x][i] != father) { int y = edg[x][i]; dp1(y, x); ddo[x] = max(ddo[x], ddo[y] + 1); cnt[x] += cnt[y]; } } typedef pair<int, int> pii; pii max(const pii &a, int b) { if(b < a.second) return a; else if(b > a.first) return make_pair(b, a.first); else return make_pair(a.first, b); } void dp2(int x, int father) { pii mx(0, 0); for(int i = 0; i < edg[x].size(); i++) if(edg[x][i] != father) mx = max(mx, ddo[edg[x][i]]); for(int i = 0; i < edg[x].size(); i++) if(edg[x][i] != father) { int y = edg[x][i]; dup[y] = max(dup[x], mx.first == ddo[y] ? mx.second : mx.first) + 1; dp2(y, x); } } void work() { dp1(1, 0); dp2(1, 0); // for(int i = 1; i <= n; i++) // cout << i << ' ' << ddo[i] << ' ' << dup[i] << ' ' << cnt[i] << endl; for(int i = 1; i <= n; i++) { pii mx(0, 0); for(int j = 0; j < edg[i].size(); j++) if(edg[i][j] != fa[i]) mx = max(mx, ddo[edg[i][j]]); mx = max(mx, dup[i]); int high = min(mx.second + 1, mx.first - 1); int low = high + 1; if(!iss[i]) { for(int j = 0; j < edg[i].size(); j++) if(edg[i][j] != fa[i]) if(cnt[edg[i][j]]) low = min(low, ddo[edg[i][j]]); if(m - cnt[i]) low = min(low, dup[i]); } else low = 0; ans += high - low + 1; } ans = ans + 1; cout << ans << endl; } int main() { ios::sync_with_stdio(false); // freopen("1.in", "r", stdin); // freopen("1.out", "w", stdout); cin >> n; for(int i = 1; i <= n - 1; i++) { int x, y; cin >> x >> y; edg[x].push_back(y), edg[y].push_back(x); } static char ss[MAXN]; cin >> (ss + 1); for(int i = 1; i <= n; i++) if(ss[i] == '1') iss[i] = true, spe.push_back(i); m = spe.size(); work(); return 0; }
Submission Info
Submission Time | |
---|---|
Task | F - Black Radius |
User | ct123098 |
Language | C++14 (GCC 5.4.1) |
Score | 1900 |
Code Size | 2301 Byte |
Status | AC |
Exec Time | 198 ms |
Memory | 29944 KB |
Judge Result
Set Name | Sample | Subtask | All | ||||||
---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 1300 / 1300 | 600 / 600 | ||||||
Status |
|
|
|
Set Name | Test Cases |
---|---|
Sample | 0_00.txt, 0_01.txt, 0_02.txt |
Subtask | 0_01.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt |
All | 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 2_00.txt, 2_01.txt, 2_02.txt, 2_03.txt, 2_04.txt, 2_05.txt, 2_06.txt, 2_07.txt, 2_08.txt, 2_09.txt, 2_10.txt, 2_11.txt, 2_12.txt, 2_13.txt, 2_14.txt, 2_15.txt, 2_16.txt, 2_17.txt, 2_18.txt, 2_19.txt, 2_20.txt, 2_21.txt, 2_22.txt, 2_23.txt, 2_24.txt, 2_25.txt, 2_26.txt, 2_27.txt, 2_28.txt, 2_29.txt, 2_30.txt, 2_31.txt, 2_32.txt, 2_33.txt, 2_34.txt, 2_35.txt, 2_36.txt, 2_37.txt, 2_38.txt, 2_39.txt, 2_40.txt, 2_41.txt, 2_42.txt, 2_43.txt, 2_44.txt, 2_45.txt, 2_46.txt, 2_47.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_00.txt | AC | 4 ms | 6400 KB |
0_01.txt | AC | 4 ms | 6400 KB |
0_02.txt | AC | 4 ms | 6400 KB |
1_00.txt | AC | 4 ms | 6400 KB |
1_01.txt | AC | 165 ms | 29944 KB |
1_02.txt | AC | 163 ms | 24824 KB |
1_03.txt | AC | 137 ms | 15736 KB |
1_04.txt | AC | 104 ms | 16116 KB |
1_05.txt | AC | 77 ms | 16500 KB |
1_06.txt | AC | 129 ms | 15860 KB |
1_07.txt | AC | 122 ms | 15988 KB |
1_08.txt | AC | 123 ms | 17012 KB |
1_09.txt | AC | 162 ms | 22900 KB |
1_10.txt | AC | 144 ms | 21752 KB |
1_11.txt | AC | 157 ms | 29812 KB |
1_12.txt | AC | 120 ms | 18932 KB |
1_13.txt | AC | 155 ms | 22264 KB |
1_14.txt | AC | 133 ms | 18548 KB |
1_15.txt | AC | 137 ms | 17272 KB |
1_16.txt | AC | 137 ms | 19700 KB |
1_17.txt | AC | 144 ms | 21240 KB |
1_18.txt | AC | 150 ms | 15736 KB |
1_19.txt | AC | 137 ms | 15732 KB |
1_20.txt | AC | 135 ms | 15732 KB |
1_21.txt | AC | 142 ms | 16632 KB |
1_22.txt | AC | 110 ms | 16116 KB |
1_23.txt | AC | 116 ms | 15860 KB |
1_24.txt | AC | 123 ms | 16884 KB |
1_25.txt | AC | 115 ms | 18164 KB |
1_26.txt | AC | 110 ms | 15860 KB |
1_27.txt | AC | 105 ms | 16116 KB |
1_28.txt | AC | 105 ms | 16628 KB |
1_29.txt | AC | 122 ms | 19316 KB |
2_00.txt | AC | 4 ms | 6400 KB |
2_01.txt | AC | 178 ms | 28416 KB |
2_02.txt | AC | 164 ms | 26240 KB |
2_03.txt | AC | 160 ms | 26108 KB |
2_04.txt | AC | 151 ms | 21632 KB |
2_05.txt | AC | 141 ms | 21888 KB |
2_06.txt | AC | 141 ms | 23420 KB |
2_07.txt | AC | 189 ms | 14848 KB |
2_08.txt | AC | 146 ms | 14848 KB |
2_09.txt | AC | 132 ms | 15104 KB |
2_10.txt | AC | 110 ms | 15096 KB |
2_11.txt | AC | 103 ms | 15096 KB |
2_12.txt | AC | 107 ms | 15608 KB |
2_13.txt | AC | 81 ms | 15604 KB |
2_14.txt | AC | 88 ms | 15604 KB |
2_15.txt | AC | 88 ms | 16244 KB |
2_16.txt | AC | 144 ms | 14976 KB |
2_17.txt | AC | 115 ms | 14976 KB |
2_18.txt | AC | 115 ms | 15104 KB |
2_19.txt | AC | 115 ms | 14976 KB |
2_20.txt | AC | 134 ms | 14976 KB |
2_21.txt | AC | 128 ms | 15732 KB |
2_22.txt | AC | 187 ms | 16000 KB |
2_23.txt | AC | 172 ms | 16768 KB |
2_24.txt | AC | 159 ms | 16888 KB |
2_25.txt | AC | 198 ms | 22400 KB |
2_26.txt | AC | 153 ms | 20992 KB |
2_27.txt | AC | 155 ms | 22900 KB |
2_28.txt | AC | 146 ms | 20352 KB |
2_29.txt | AC | 164 ms | 26752 KB |
2_30.txt | AC | 151 ms | 24704 KB |
2_31.txt | AC | 154 ms | 20608 KB |
2_32.txt | AC | 121 ms | 15488 KB |
2_33.txt | AC | 125 ms | 18172 KB |
2_34.txt | AC | 144 ms | 20352 KB |
2_35.txt | AC | 139 ms | 23296 KB |
2_36.txt | AC | 145 ms | 14720 KB |
2_37.txt | AC | 131 ms | 15104 KB |
2_38.txt | AC | 147 ms | 16508 KB |
2_39.txt | AC | 121 ms | 14976 KB |
2_40.txt | AC | 105 ms | 15096 KB |
2_41.txt | AC | 128 ms | 15228 KB |
2_42.txt | AC | 98 ms | 14968 KB |
2_43.txt | AC | 148 ms | 21632 KB |
2_44.txt | AC | 100 ms | 15096 KB |
2_45.txt | AC | 88 ms | 15604 KB |
2_46.txt | AC | 88 ms | 15736 KB |
2_47.txt | AC | 122 ms | 19828 KB |