#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define int long long
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mxN = (int)1e5+10;
const int LINF = (int)1e18;
int n, m, x, y;
vector<int> adj[mxN];
int a[mxN], b[mxN], c[mxN], node[mxN];
bool bad[20][20];
bool dfs(int s, int t, int v, int p){
if(s==t) return true;
for(auto u : adj[s]){
if(u==p) continue;
if(dfs(u,t,v,s)){ node[u]+=v; return 1; }
}
return 0;
}
void add(int s, int t, int v){
// this can be done in O(logN) or O(1) with sparse tables...
node[s]+=v;
dfs(s,t,v,-1);
}
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n; int ans = 0;
for(int i = 1; i < n; i++){
int x, y; cin >> x >> y;
adj[x].pb(y), adj[y].pb(x);
}
cin >> m;
for(int i = 0; i < m; i++) cin >> a[i] >> b[i] >> c[i];
for(int i = 0; i < m; i++){
for(int j = i+1; j < m; j++){
add(a[i],b[i],1), add(a[j],b[j],1);
bad[i][j]=bad[j][i]=(*max_element(node+1,node+n+1)==2);
add(a[i],b[i],-1), add(a[j],b[j],-1);
}
}
for(int mask = 0; mask < (1<<m); mask++){
vector<int> v; v.clear(); bool ok = 0;
for(int i = 0; i < m; i++) if((mask>>i)&1) v.pb(i);
for(int i = 0; i < sz(v); i++)
for(int j = i+1; j < sz(v); j++)
ok|=bad[i][j];
if(ok) continue; int sum = 0;
for(auto u : v) sum+=c[u];
ans = max(ans, sum);
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1053 ms |
12276 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |