답안 #786645

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
786645 2023-07-18T10:45:58 Z Dan4Life Designated Cities (JOI19_designated_cities) C++17
0 / 100
7 ms 9940 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define sz(a) (int)a.size()
const int mxN = (int)1e5+10;
int n, q, ans[mxN];
vector<array<int,2>> adj[mxN], tadj[mxN];

void rem(int s, int p){
	for(auto &[u,w] : adj[s]){
		if(u==p) continue;
		w = 0; rem(u,s);
	}
}

int dfs(int s, int p=-1){
	int tot = 0;
	for(auto [u,w] : adj[s])
		if(u!=p) tot+=w+dfs(u,s);
	return tot;
}

int getRoot(){
	pair<int,int> best = {0,-1};
	for(int i = 0; i < n; i++)
		best=max(best,{dfs(i),i});
	return best.second;
}

int32_t main()
{
	cin >> n; int tot = 0;
	for(int i = 0; i < n-1; i++){
		int a, b, c, d;
		cin >> a >> b >> c >> d; 
		a--,b--,swap(c,d); tot+=c+d;
		adj[a].pb({b,c}); adj[b].pb({a,d});
		tadj[a].pb({b,c}); tadj[b].pb({a,d});
	}
	for(int x = 1; x <= n; x++){
		rem(getRoot(),-1);
		for(int i = 0; i < n; i++)
			for(auto [b,c] : adj[i]) ans[x]+=c;
	}
	cin >> q;
	while(q--){ int x; cin >> x,cout << ans[x] << "\n";}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5012 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Incorrect 3 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Runtime error 7 ms 9940 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Runtime error 7 ms 9940 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5012 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Incorrect 3 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Runtime error 7 ms 9940 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5012 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Incorrect 3 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -