#include "roads.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<ii> vi;
typedef pair<ii,int> iii;
const int MAX_N=2e5+10;
bool vis[MAX_N];
int pa[MAX_N];
vector<vi> G;
int n;
void init(int n){
for(int i=0;i<n;i++)
pa[i]=i;
}
int findset(int x){
if(pa[x]==x) return x;
return pa[x]=findset(pa[x]);
}
bool issameset(int i,int j){
return findset(i)==findset(j);
}
void unionset(int i,int j){
if(!issameset(i,j)){
int u=findset(i),v=findset(j);
pa[u]=v;
}
}
ll dp[MAX_N];
int path[MAX_N];
ll knap(int u,int p){
if(dp[u]!=-1) return dp[u];
ll ans=0;
for(auto &v:G[u]){
if(v.first!=p){
ans+=knap(v.first,u)+v.second;
}
}
ll s=ans;
int id=-1;
for(auto &v:G[u]){
if(v.first!=p){
ll rp=s;
rp=rp-dp[v.first]-v.second;
for(auto &z:G[v.first]){
if(z.first!=u){
rp+=dp[z.first]+z.second;
}
}
if(ans>rp){
ans=rp;
id=v.first;
}
}
}
path[u]=id;
return dp[u]=ans;
}
std::vector<long long> minimum_closure_costs(int N, std::vector<int> U,
std::vector<int> V,
std::vector<int> W) {
n=N;
vector<ll> res;
vector<iii> ed;
G.resize(n+1);
ll s=0;
for(int i=0;i<n-1;i++){
int a=U[i],b=V[i],w=W[i];
G[a].push_back(ii(b,w));
G[b].push_back(ii(a,w));
ed.push_back(iii(ii(a,b),w));
s+=w;
}
res.push_back(s);
init(n);
for(int i=0;i<n-1;i++){
memset(dp,-1,sizeof dp);
res.push_back(knap(findset(0),findset(0)));
for(int j=0;j<n;j++){
if(path[j]!=-1) unionset(j,path[j]);
}
G.clear(); G.resize(n+1);
for(int j=0;j<ed.size();j++){
int u=findset(ed[j].first.first);
int v=findset(ed[j].first.second);
ll w=ed[j].second;
if(u!=v){
G[u].push_back(ii(v,w));
G[v].push_back(ii(u,w));
}
}
}
return res;
}
Compilation message
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:84:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int j=0;j<ed.size();j++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1836 KB |
Output is correct |
2 |
Correct |
258 ms |
2124 KB |
Output is correct |
3 |
Correct |
289 ms |
2116 KB |
Output is correct |
4 |
Correct |
274 ms |
2244 KB |
Output is correct |
5 |
Correct |
7 ms |
1868 KB |
Output is correct |
6 |
Correct |
10 ms |
1848 KB |
Output is correct |
7 |
Correct |
10 ms |
1896 KB |
Output is correct |
8 |
Correct |
213 ms |
1996 KB |
Output is correct |
9 |
Correct |
288 ms |
2104 KB |
Output is correct |
10 |
Correct |
9 ms |
1836 KB |
Output is correct |
11 |
Correct |
11 ms |
1868 KB |
Output is correct |
12 |
Execution timed out |
2092 ms |
9284 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1836 KB |
Output is correct |
2 |
Execution timed out |
2055 ms |
18212 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1832 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
1 ms |
1868 KB |
Output is correct |
4 |
Incorrect |
7 ms |
1868 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1832 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
1 ms |
1868 KB |
Output is correct |
4 |
Incorrect |
7 ms |
1868 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2027 ms |
13124 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2027 ms |
13124 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1836 KB |
Output is correct |
2 |
Correct |
258 ms |
2124 KB |
Output is correct |
3 |
Correct |
289 ms |
2116 KB |
Output is correct |
4 |
Correct |
274 ms |
2244 KB |
Output is correct |
5 |
Correct |
7 ms |
1868 KB |
Output is correct |
6 |
Correct |
10 ms |
1848 KB |
Output is correct |
7 |
Correct |
10 ms |
1896 KB |
Output is correct |
8 |
Correct |
213 ms |
1996 KB |
Output is correct |
9 |
Correct |
288 ms |
2104 KB |
Output is correct |
10 |
Correct |
9 ms |
1836 KB |
Output is correct |
11 |
Correct |
11 ms |
1868 KB |
Output is correct |
12 |
Execution timed out |
2092 ms |
9284 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |