제출 #114311

#제출 시각아이디문제언어결과실행 시간메모리
114311tincamateiDesignated Cities (JOI19_designated_cities)C++14
7 / 100
411 ms31224 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 200000;

struct Edge {
	int a, b, c, d;
	
	int other(int nod) {
		return a ^ b ^ nod;
	}

	int cost(int src) {
		if(src == a)
			return c;
		else
			return d;
	}
} edges[MAX_N - 1];

vector<int> graph[1+MAX_N];
long long rez[1+MAX_N];

void calcRoot(int nod, int papa = 0) {
	for(auto it: graph[nod]) {
		int son = edges[it].other(nod);
		if(son != papa) {
			rez[1] += edges[it].cost(nod);
			calcRoot(son, nod);
		}
	}
}

long long calcBestPathFor1(int nod, int papa = 0, long long extra = 0LL) {
	long long rez = extra;
	for(auto it: graph[nod]) {
		int son = edges[it].other(nod);
		if(son != papa) {
			long long sonRez = calcBestPathFor1(son, nod, extra - edges[it].cost(nod) + edges[it].cost(son));
			rez = min(rez, sonRez);
		}
	}
	return rez;
}

int main() {
	int N, Q;
	int leaves = 0;

	scanf("%d", &N);
	for(int i = 0; i < N - 1; ++i) {
		scanf("%d%d%d%d", &edges[i].a, &edges[i].b, &edges[i].c, &edges[i].d);
		graph[edges[i].a].push_back(i);
		graph[edges[i].b].push_back(i);
	}

	for(int i = 1; i <= N; ++i)
		if(graph[i].size() == 1)
			++leaves;
	
	rez[1] = calcBestPathFor1(1);
	calcRoot(1);

	scanf("%d", &Q);
	for(int i = 0; i < Q; ++i) {
		int x;
		scanf("%d", &x);
		printf("%lld\n", rez[x]);
	}
	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:51:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:53:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d%d", &edges[i].a, &edges[i].b, &edges[i].c, &edges[i].d);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &Q);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &x);
   ~~~~~^~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...