Submission #395596

# Submission time Handle Problem Language Result Execution time Memory
395596 2021-04-28T16:31:50 Z garnab27 Traffic (IOI10_traffic) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

using vi = vector<int>;
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair
#define nline '\n'

string to_string(string s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

void setIO(string name = "") {
	cin.tie(0)->sync_with_stdio(0);
	if (sz(name)) {
		freopen((name+".in").c_str(), "r", stdin);
		freopen((name+".out").c_str(), "w", stdout);
	}
}

static int N,P[1000000],S[1000000],D[1000000];

vector<ll > sum;
vector<ll > ans;
vector<int> g[1000006];

ll dfs1(int u, int p) {
	sum[u] += P[u];

	for(int v: g[u]) {
		if(v != p) {
			sum[u] += dfs1(v, u);
		} 
	}

	return sum[u];
}

void dfs2(int u, int p, ll psum) {
	
	// if(p != -1)
	// 	debug(u, p, psum, P[p]);

	ans[u] = 0;

	ll csum = P[u];

	if(p != -1) {
		ans[u] = max(ans[u], psum);
		csum += psum;
	}

	for(int v: g[u]) {
		if(v != p) {
			csum += sum[v];
			ans[u] = max(ans[u], sum[v]);
		}
	}

	for(int v: g[u]) {
		if(v != p) {
			dfs2(v, u, csum - sum[v]);
		}
	}
}

int LocateCentre(int N, int pp[], int S[], int D[]) {
   	
   	sum.resize(N);
   	ans.resize(N);
   	
   	for(int i=0;i<N-1;i++) {
   		g[S[i]].pb(D[i]);
   		g[D[i]].pb(S[i]);
   	}

   	dfs1(0, -1);

   	// debug(sum);

   	dfs2(0, -1, 0);

   	// debug(ans);

   	int id = 0, mn = ans[0];

   	for(int i=1;i<N;i++) {
   		if(mn > ans[i]) {
   			mn = ans[i];
   			id = i;
   		}
   	}

   	return id;
}

int main() {
	setIO();
	int i;
	scanf("%d",&N);
	for (i=0;i<N;i++) scanf("%d",&P[i]);
	for (i=0;i<N-1;i++) scanf("%d%d",&S[i],&D[i]);
	int r = LocateCentre(N,P,S,D);
	printf("%d\n",r);
	
}

Compilation message

traffic.cpp: In function 'void setIO(std::string)':
traffic.cpp:66:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   66 |   freopen((name+".in").c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
traffic.cpp:67:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   67 |   freopen((name+".out").c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
traffic.cpp: In function 'int main()':
traffic.cpp:150:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  150 |  scanf("%d",&N);
      |  ~~~~~^~~~~~~~~
traffic.cpp:151:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  151 |  for (i=0;i<N;i++) scanf("%d",&P[i]);
      |                    ~~~~~^~~~~~~~~~~~
traffic.cpp:152:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  152 |  for (i=0;i<N-1;i++) scanf("%d%d",&S[i],&D[i]);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~
/tmp/cclFYkjE.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccUxzn5k.o:traffic.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status