Submission #155598

#TimeUsernameProblemLanguageResultExecution timeMemory
155598HellAngelMergers (JOI19_mergers)C++14
0 / 100
147 ms27348 KiB
#include <bits/stdc++.h> #define int long long using namespace std; const int maxn = 5e5 + 7; const int maxState = (1 << 8); int n, m, u, v, Min[maxn], Max[maxn], cnt, sum[maxn], st[maxn], last1 = 0, en[maxn], ITSum[maxn * 4], ITMax[4 * maxn]; vector<int> vt[maxn], col, ahihi; int ans = 0; set<int> St; bool ok[maxn]; /// first: sum /// second: last void Update(int id, int l, int r, int pos, pair<int, int> x) { if(l == r) { ITSum[id] = x.first; ITMax[id] = x.second; return; } int mid = l + r >> 1; if(pos <= mid) Update(id * 2,l , mid, pos, x); else Update(id * 2 + 1, mid + 1, r, pos, x); ITSum[id] = ITSum[id * 2] + ITSum[id * 2 + 1]; ITMax[id] = max(ITMax[id * 2], ITMax[id * 2 + 1]); } pair<int, int> Get(int id, int l, int r, int u, int v) { if(l > v || r < u) return {0, 0}; if(u <= l && r <= v) { return {ITSum[id], ITMax[id]}; } int mid = l + r >> 1; pair<int, int> gau1 = Get(id * 2, l, mid, u, v); pair<int, int> gau2 = Get(id * 2 + 1, mid + 1, r, u, v); return {gau1.first + gau2.first, max(gau1.second, gau2.second)}; } void DFS(int u, int p) { st[u] = ++cnt; for(auto v: vt[u]) { if(v != p) DFS(v, u); } en[u] = cnt; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); if(fopen("test.inp", "r")) freopen("test.inp", "r", stdin), freopen("test.out", "w", stdout); cin >> n >> m; for(int i = 1; i <= m; i++) Min[i] = INT_MAX; for(int i = 1; i < n; i++) { int u, v; cin >> u >> v; vt[u].push_back(v); vt[v].push_back(u); } DFS(1, 1); for(int i = 1; i <= n; i++) { ahihi.push_back(i); int x; cin >> x; Max[x] = max(Max[x], st[i]); Min[x] = min(Min[x], st[i]); sum[x]++; } for(int i = 1; i <= m; i++) Update(1, 1, n, Min[i], {sum[i], Max[i]}); vector<int> prvans, ans; for(int i = 1; i <= n; i++) { pair<int, int> gau = Get(1, 1, n, st[i], en[i]); if(gau.second <= en[i] && gau.first == en[i] - st[i] + 1) { ok[i] = true; prvans.push_back(i); } } sort(prvans.begin(), prvans.end(), [](const int &a, const int &b) { return st[a] < st[b]; }); for(int i = 0; i + 1 < prvans.size(); i++) { if(en[prvans[i]] < st[prvans[i + 1]]) { ans.push_back(i); } } ans.push_back(*prvans.rbegin()); int ok = 0; for(auto i: prvans) { if(st[i] <= st[ans[0]] && en[i] >= en[ans.back()]) ok++; } cout << (ok > 1) + ans.size() - 1; }

Compilation message (stderr)

mergers.cpp: In function 'void Update(long long int, long long int, long long int, long long int, std::pair<long long int, long long int>)':
mergers.cpp:21:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid = l + r >> 1;
               ~~^~~
mergers.cpp: In function 'std::pair<long long int, long long int> Get(long long int, long long int, long long int, long long int, long long int)':
mergers.cpp:35:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid = l + r >> 1;
               ~~^~~
mergers.cpp: In function 'int32_t main()':
mergers.cpp:89:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i + 1 < prvans.size(); i++)
                    ~~~~~~^~~~~~~~~~~~~~~
mergers.cpp:54:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     if(fopen("test.inp", "r")) freopen("test.inp", "r", stdin), freopen("test.out", "w", stdout);
                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:54:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...