#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, int>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5+5;
const int M = 14;
const ll inf = 1e17;
const int base = 350;
const ll mod = 998244353;
int n, t, m, k, q, d[N], b[N], c[N], deg[N];
ll ans, tong, a[N];
vector<int> gr[N], ask[N];
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
string s;
struct BIT
{
int n;
vector<int> fe;
BIT(int _n)
{
n = _n;
fe.resize(n+1, 0);
}
void add(int id, int x)
{
for(; id <= n; id += id & -id)fe[id] += x;
}
int get(int id)
{
int total = 0;
for(; id; id -= id & -id)total += fe[id];
return total;
}
};
struct node
{
int v, c, d;
node(){}
node(int _v, int _c, int _d)
{
v = _v;
c = _c;
d = _d;
}
};
vector<node> vi, adj[N];
void dfs(int u, int p = 0)
{
for(node x: adj[u])
{
if(x.v == p)continue;
dfs(x.v, u);
d[u] += d[x.v] + x.c;
}
}
void dfs1(int u, ll sum, int p = 0)
{
sum += d[u];
a[1] = min(a[1], sum);
for(node x: adj[u])
{
if(x.v == p)continue;
dfs1(x.v, sum-d[x.v]-x.c+x.d, u);
}
}
void sol()
{
cin >> n;
for(int i = 1; i < n; i ++)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
adj[a].pb(node(b, c, d));
adj[b].pb(node(a, d, c));
++deg[a];
++deg[b];
}
a[1] = inf;
dfs(1);
dfs1(1, 0);
priority_queue< pll, vector<pll>, greater<pll> > pq;
for(int i = 1; i <= n; i ++)
{
if(deg[i] == 1)
{
pq.push({adj[i][0].d, i});
}
}
while(pq.size() > 2)
{
pii u = pq.top();
pq.pop();
int cnt = 0;
node par = node(-1, 0, 0);
//cout << u.se <<" "<<u.fi<<'\n';
for(node x: adj[u.se])
{
if(deg[x.v] > 0)
{
--deg[x.v];
--deg[u.se];
++cnt;
if(deg[x.v] == 1)par = x;
else
{
//cout <<"res "<< pq.size() <<" "<<a[pq.size()+1]+u.fi<<'\n';
a[pq.size()] = a[pq.size()+1]+u.fi;
}
}
}
//assert(cnt <= 1);
//cout << "par " << par.v<<" "<<par.d << '\n';
if(par.v != -1)
for(node x: adj[par.v])
if(deg[x.v] > 0)
pq.push({x.d+u.fi, par.v});
}
cin >> m;
while(m -- > 0)
{
cin >> k;
cout << a[k] << '\n';
}
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
return 0;
}
Compilation message
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:140:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:141:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14540 KB |
Output is correct |
2 |
Incorrect |
7 ms |
14412 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Incorrect |
259 ms |
26436 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Incorrect |
259 ms |
26536 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14540 KB |
Output is correct |
2 |
Incorrect |
7 ms |
14412 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Incorrect |
259 ms |
26436 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14540 KB |
Output is correct |
2 |
Incorrect |
7 ms |
14412 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |