# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
410552 | Richw818 | Traffic (IOI10_traffic) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
* Written by: Richw818
* Lang: C++17
* main.cpp
*/
#include <bits/stdc++.h>
using namespace std;
//Macros
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define pb push_back
#define pf push_front
#define eb emplace_back
#define ef emplace_front
#define str string
#define fi first
#define se second
#define REP(i, n) for(int i = 0; i < (int)n; ++i)
#define sz(a) (int)(a.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
//IO
template<class H, class T> void read(pair<H, T> &p);
template<class T> void read(vector<T>& v);
template<class T> void read(T& t){
cin >> t;
}
template<class H, class... T> void read(H& h, T&... t){
read(h);
read(t...);
}
template<class T> void read(vector<T>& v){
for(auto &t : v)
read(t);
}
template<class H, class T> void read(pair<H, T> &p){
read(p.fi, p.se);
}
void setIn(str s){
freopen(s.c_str(), "r", stdin);
}
void setOut(str s){
freopen(s.c_str(), "w", stdout);
}
void setIO(str s = ""){
if(!sz(s))
return;
setIn(s+".in");
setOut(s+".out");
}
//Debug
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
template<class H, class... T> void dbg(H& h, T&... t){
dbg(h);
dbg(t...);
}
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "[" << #x << "] = ["; _print(x);
#else
#define dbg(x...)
#endif
//Constants
ll MODS[2] = {1000000007, 998244353};
const ll MOD = MODS[0];
const ll INF = (ll)1e18;
const double PI = acos(-1.0);
const int MAXN = (int)1e6+7;
//Code
vector<int> pop;
vector<int> tree[MAXN];
int cong[MAXN];
bitset<MAXN> vis;
int traff = INT_MAX, city = -1;
void dfs1(int n){
vis[n] = true;
cong[n] += pop[n];
for(int adj : tree[n]){
if(!vis[adj]){
dfs1(adj);
cong[n] += cong[adj];
}
}
}
void dfs2(int n, int x){
vis[n] = true;
int curr = x;
for(int adj : tree[n]){
if(!vis[adj]){
dfs2(adj, x + cong[n] - cong[adj]);
curr = max(curr, cong[adj]);
}
}
if(curr < traff){
traff = curr;
city = n;
}
}
void solve(){
int n; read(n);
pop.resize(n); read(pop);
REP(i, n-1){
int s, d; read(s, d);
tree[s].eb(d);
tree[d].eb(s);
}
dfs1(0);
vis.reset();
dfs2(0, 0);
cout << city;
}
int main(){
setIO();
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/