#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll // delete if causing problems
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define mod(a) (a+MOD)%MOD
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
const int mxN = (int)3e5+10; // change when needed!
int n, m, a[mxN], b[mxN], c[mxN], vis[mxN];
vector<int> adj[mxN];
bool dfs(int s, int t, int p){
if(s==t) return true;
for(auto u : adj[s]){
if(u==p) continue;
if(dfs(u,t,s)){
vis[u]++; return true;
}
}
return false;
}
void solve()
{
cin >> n; int ans = 0;
for(int i = 1; i < n; i++){
int a, b; cin >> a >> b;
adj[a].pb(b), adj[b].pb(a);
}
cin >> m;
for(int i = 0; i < m; i++){
cin >> a[i] >> b[i] >> c[i];
}
for(int mask = 0; mask < (1<<m); mask++){
int tot = 0;
fill(vis,vis+n+1,0);
for(int i = 0; i < m; i++)
if((mask>>i)&1) tot+=c[i], vis[a[i]]++, dfs(a[i],b[i], -1);
bool ok = 1;
for(int i = 1; i <= n; i++)
if(vis[i]>1) ok=0;
if(ok) ans = max(ans, tot);
}
cout << ans;
}
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
int t = 1; //cin >> t;
while(t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
178 ms |
7444 KB |
Output is correct |
5 |
Execution timed out |
1087 ms |
13132 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
62 ms |
16808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
178 ms |
7444 KB |
Output is correct |
5 |
Execution timed out |
1087 ms |
13132 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
178 ms |
7444 KB |
Output is correct |
5 |
Execution timed out |
1087 ms |
13132 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |