Submission #313870

#TimeUsernameProblemLanguageResultExecution timeMemory
313870nikatamlianiSplit the Attractions (IOI19_split)C++14
22 / 100
144 ms12644 KiB
#include <bits/stdc++.h> #include "split.h" using namespace std; const int N = 2e5 + 10; int in[N], out[N], sub[N]; int timer, countA, countB, countC, aa = 1, bb = 2, cc = 3; #define vi vector < int > int dfs(int x, int p, vector < vi > &edges) { in[x] = ++timer; sub[x] = 1; for(int to : edges[x]) { if(to != p) { sub[x] += dfs(to, x, edges); } } out[x] = ++timer; return sub[x]; } int find_good(int x, int p, vector < vi > &edges) { int n = edges.size(); for(int to : edges[x]) { if(to != p && sub[to] >= countA && sub[to] <= countA + countC) { return x; } if(to == p && n - sub[x] >= countA && n - sub[x] <= countA + countC) { return x; } } for(int to : edges[x]) { if(to != p && sub[to] > countA + countC) { return find_good(to, x, edges); } } return -1; } void find_children(int x, int p, int v, vi &ans, vector < vi > &edges) { if(in[x] >= in[v] && out[v] >= out[x]) { if(countA > 0) { ans[x] = aa; --countA; } } else { if(countB > 0) { ans[x] = bb; --countB; } } for(int to : edges[x]) { if(to != p) { find_children(to, x, v, ans, edges); } } } void dfs_graph(int x, vector < vi > &edges, vi &vis, vi &ans) { if(vis[x]) return; vis[x] = 1; if(countB > 0) { ans[x] = 2; --countB; } for(int to : edges[x]) dfs_graph(to, edges, vis, ans); } void dfs_(int x, int &cnt, int which, vector < vi > edges, vi vis, vi ans) { if(vis[x]) return; vis[x] = 1; if(cnt > 0) { ans[x] = which; --cnt; } for(int to : edges[x]) dfs_(to, cnt, which, edges, vis, ans); } vi find_split(int n, int a, int b, int c, vi p, vi q) { vector < vi > edges(n, vi(0, 0)); vi ans(n, 0), vis(n, 0); countA = a, countB = b, countC = c; if(a == 1) { for(int i = 0; i < q.size(); ++i) { edges[q[i]].push_back(p[i]); edges[p[i]].push_back(q[i]); } dfs_(0, countB, 2, edges, vis, ans); for(int i = 0; i < n; ++i) { if(ans[i] == 0) { ans[i] = 1; break; } } for(int i = 0; i < n; ++i) { if(ans[i] == 0) { ans[i] = 3; } } return ans; } if(p.size() != n - 1) { for(int i = 0; i < n; ++i) { dfs_(i, countA, 1, edges, vis, ans); } for(int i = 0; i < n; ++i) { dfs_(i, countB, 2, edges, vis, ans); } for(int i = 0; i < n; ++i) { dfs_(i, countC, 3, edges, vis, ans); } return ans; } for(int i = 0; i < n - 1; ++i) { edges[p[i]].push_back(q[i]); edges[q[i]].push_back(p[i]); } if(a > b) swap(a, b), swap(aa, bb); if(b > c) swap(b, c), swap(bb, cc); if(a > b) swap(a, b), swap(aa, bb); countA = a, countB = b, countC = c; timer = 0; dfs(0, -1, edges); int good = find_good(0, -1, edges); if(good == -1) return ans; timer = 0; dfs(good, -1, edges); int subTree; for(int i = 0; i < n; ++i) { if(sub[i] >= a && sub[i] <= a + c) { subTree = i; } } find_children(good, -1, subTree, ans, edges); for(int i = 0; i < n; ++i) { if(ans[i] == 0) { ans[i] = cc; } } return ans; }

Compilation message (stderr)

split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:77:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |         for(int i = 0; i < q.size(); ++i) {
      |                        ~~^~~~~~~~~~
split.cpp:95:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   95 |     if(p.size() != n - 1) {
      |        ~~~~~~~~~^~~~~~~~
split.cpp:124:18: warning: 'subTree' may be used uninitialized in this function [-Wmaybe-uninitialized]
  124 |     find_children(good, -1, subTree, ans, edges);
      |     ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...