/*huypheu
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
*/
#include <bits/stdc++.h>
#define int long long
#define oo (LLONG_MAX/2)
using namespace std;
int dp[200007][2];
vector <pair<int,int> > ch[200007],v[200007],vn;
int par[200007];
bool vst[200007];
void dfs(int u)
{
vst[u]=1;
for(int i=0;i<v[u].size();i++)
{
int k=v[u][i].first;
if(!vst[k])
{
ch[u].push_back(v[u][i]);
par[k]=u;
dfs(k);
}
}
return ;
}
void get_ans(int u)
{
for(int i=0;i<ch[u].size();i++)
{
int k=ch[u][i].first;
get_ans(k);
}
dp[u][1]=-oo;
int tot=0;
vector <int> jo;
for(int i=0;i<ch[u].size();i++)
{
int k=ch[u][i].first;
tot+=max(dp[k][0],dp[k][1]+ch[u][i].second);
jo.push_back(max(dp[k][0],dp[k][1]+ch[u][i].second));
}
for(int i=0;i<jo.size();i++)
{
int k=ch[u][i].first;
dp[u][1]=max(dp[u][1],tot-jo[i]+dp[k][0]+ch[u][i].second);
}
dp[u][0]=tot;
return ;
}
signed main()
{
int n;
cin >> n;
for(int i=1;i<n;i++)
{
int a,b,c;
cin >> a >> b >> c;
v[a].push_back(make_pair(b,c));
v[b].push_back(make_pair(a,c));
}
int totans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++) vst[j]=0;
for(int j=1;j<=n;j++) ch[j]=vn;
for(int j=1;j<=n;j++) par[j]=0,dp[j][0]=0,dp[j][1]=0;
dfs(i);
get_ans(i);
// for(int j=1;j<=n;j++) cout << par[j] << " ";
// cout << endl;
// cout << dp[i][0] << endl;
totans=max(totans,dp[i][0]);
}
// for(int i=1;i<=n;i++) cout << dp[i][0] << " " << dp[i][1] << endl;
// cout << "concak" << endl;
cout << totans << endl;
return 0;
}
Compilation message
beads.cpp: In function 'void dfs(long long int)':
beads.cpp:27:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[u].size();i++)
~^~~~~~~~~~~~
beads.cpp: In function 'void get_ans(long long int)':
beads.cpp:42:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<ch[u].size();i++)
~^~~~~~~~~~~~~
beads.cpp:50:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<ch[u].size();i++)
~^~~~~~~~~~~~~
beads.cpp:56:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<jo.size();i++)
~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
9 ms |
9728 KB |
Output is correct |
3 |
Correct |
9 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
9 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
9 ms |
9728 KB |
Output is correct |
8 |
Correct |
9 ms |
9728 KB |
Output is correct |
9 |
Correct |
9 ms |
9728 KB |
Output is correct |
10 |
Correct |
9 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
9 ms |
9728 KB |
Output is correct |
3 |
Correct |
9 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
9 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
9 ms |
9728 KB |
Output is correct |
8 |
Correct |
9 ms |
9728 KB |
Output is correct |
9 |
Correct |
9 ms |
9728 KB |
Output is correct |
10 |
Correct |
9 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
10 ms |
9728 KB |
Output is correct |
15 |
Correct |
10 ms |
9728 KB |
Output is correct |
16 |
Correct |
12 ms |
9728 KB |
Output is correct |
17 |
Correct |
12 ms |
9728 KB |
Output is correct |
18 |
Correct |
12 ms |
9728 KB |
Output is correct |
19 |
Correct |
11 ms |
9728 KB |
Output is correct |
20 |
Correct |
11 ms |
9728 KB |
Output is correct |
21 |
Correct |
11 ms |
9728 KB |
Output is correct |
22 |
Correct |
12 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
9 ms |
9728 KB |
Output is correct |
3 |
Correct |
9 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
9 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
9 ms |
9728 KB |
Output is correct |
8 |
Correct |
9 ms |
9728 KB |
Output is correct |
9 |
Correct |
9 ms |
9728 KB |
Output is correct |
10 |
Correct |
9 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
10 ms |
9728 KB |
Output is correct |
15 |
Correct |
10 ms |
9728 KB |
Output is correct |
16 |
Correct |
12 ms |
9728 KB |
Output is correct |
17 |
Correct |
12 ms |
9728 KB |
Output is correct |
18 |
Correct |
12 ms |
9728 KB |
Output is correct |
19 |
Correct |
11 ms |
9728 KB |
Output is correct |
20 |
Correct |
11 ms |
9728 KB |
Output is correct |
21 |
Correct |
11 ms |
9728 KB |
Output is correct |
22 |
Correct |
12 ms |
9728 KB |
Output is correct |
23 |
Execution timed out |
1066 ms |
10360 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
9 ms |
9728 KB |
Output is correct |
3 |
Correct |
9 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
9 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
9 ms |
9728 KB |
Output is correct |
8 |
Correct |
9 ms |
9728 KB |
Output is correct |
9 |
Correct |
9 ms |
9728 KB |
Output is correct |
10 |
Correct |
9 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
10 ms |
9728 KB |
Output is correct |
15 |
Correct |
10 ms |
9728 KB |
Output is correct |
16 |
Correct |
12 ms |
9728 KB |
Output is correct |
17 |
Correct |
12 ms |
9728 KB |
Output is correct |
18 |
Correct |
12 ms |
9728 KB |
Output is correct |
19 |
Correct |
11 ms |
9728 KB |
Output is correct |
20 |
Correct |
11 ms |
9728 KB |
Output is correct |
21 |
Correct |
11 ms |
9728 KB |
Output is correct |
22 |
Correct |
12 ms |
9728 KB |
Output is correct |
23 |
Execution timed out |
1066 ms |
10360 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |