Submission #338300

#TimeUsernameProblemLanguageResultExecution timeMemory
338300souvenir_vaynePapričice (COCI20_papricice)C++14
0 / 110
10 ms12140 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <chrono> #define pb push_back #define INF 0x3f3f3f3f #define LINF 0x3f3f3f3f3f3f3f3f #define int long long #define endl '\n' #define ll long long #define f first #define fin cin #define fout cout #define s second #define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0) #define debug(x) cout << "DEBUG " << x << endl #define debug2(x, y) cout << "DEBUG " << x << " " << y << endl #define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl #define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl #define all(x) x.begin(), x.end() #define left vadia #define lb lower_bound #define right puta using namespace std; using namespace __gnu_pbds; void setIO(string s) { ios_base::sync_with_stdio(0); cin.tie(0); freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str( ),"w",stdout); } typedef pair<ll, ll> pii; typedef vector<vector<char>> mat; typedef pair<int, string> pis; const ll mod = 1e9+7; typedef vector<int> vi; typedef pair<int, pair<int, int>> piii; const int MAXN = 5e5+5; int sz[MAXN], n; vector<int> g[MAXN]; void get(int u, int p) { sz[u] = 1; for(int &i : g[u]) if(i != p) { get(i, u); sz[u] += sz[i]; } } multiset<int> a, b; int ans = INT_MAX; int calc(int y, int x) { int a1 = x, a2 = y - x, a3 = n - y; return max({abs(a1 - a2), abs(a1 - a3), abs(a2 - a3)}); } int calc2(int y, int x) { return max({abs(x - y), abs( (n - x - y) - x ), abs( (n - x - y) - y )}); } void dfs(int u, int p) { if(u != 1) { int ini = sz[u], mid, end = n, best_calc = INT_MAX, best = -190; while(ini <= end) { mid = (ini + end) >> 1; if(calc(mid, sz[u]) > calc(mid+1, sz[u])) { end = mid-1; if(calc(mid, sz[u]) < best_calc) best = mid, best_calc = calc(mid, sz[u]); } else { ini = mid+2; if(calc(mid+1, sz[u]) < best_calc) best = mid+1, best_calc = calc(mid, sz[u]); } } //debug3("Hi", u, best); auto it = a.lower_bound(best); if(it != a.end()) ans = min(ans, calc(*it, sz[u])); if(it != a.begin()) { it--; ans = min(ans, calc(*it, sz[u])); } ini = 0, mid, end = n, best_calc = INT_MAX, best = 0; while(ini <= end) { mid = (ini + end) >> 1; if(calc2(mid, sz[u]) > calc2(mid+1, sz[u])) { end = mid-1; if(calc2(mid, sz[u]) < best_calc) best = mid, best_calc = calc2(mid, sz[u]); } else { ini = mid+2; if(calc2(mid+1, sz[u]) < best_calc) best = mid+1, best_calc = calc2(mid, sz[u]); } } //debug3("Hello", u, best); auto it2 = a.lower_bound(best); if(it2 != a.end()) ans = min(ans, calc2(*it2, sz[u])); if(it2 != a.begin()) { it2--; ans = min(ans, calc2(*it2, sz[u])); } } a.insert(sz[u]); for(int &i : g[u]) if(i != p) dfs(i, u); a.erase(a.find(sz[u])); b.insert(sz[u]); } int32_t main() { cin >> n; for(int i = 0; i < n-1; i++) { int a, b; cin >> a >> b; g[a].pb(b); g[b].pb(a); } get(1, 1); dfs(1, 1); cout << ans << endl; }

Compilation message (stderr)

papricice.cpp: In function 'void dfs(long long int, long long int)':
papricice.cpp:92:29: warning: right operand of comma operator has no effect [-Wunused-value]
   92 |         ini = 0, mid, end = n, best_calc = INT_MAX, best = 0;
      |                             ^
papricice.cpp: In function 'void setIO(std::string)':
papricice.cpp:27:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   27 |   freopen((s+".in").c_str(),"r",stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
papricice.cpp:28:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   28 |   freopen((s+".out").c_str( ),"w",stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...