#include <vector>
#include <algorithm>
#include <queue>
#include "dreaming.h"
using namespace std;
int const N=1e5+10;
vector<pair<int,int>>nei[N]={};
vector<int>heads;
int vis[N]={};
int cost[N]={};
void dfs(int n)
{
vis[n]=1;
for (auto [i,w]:nei[n])
{
if (!vis[i])
{
cost[n]+=w;
dfs(i);
cost[n]+=cost[i];
}
}
}
vector<pair<int,int>>cur={};
bool w1=0;
void getroot(int n,int pa,int root)
{
int f=cost[root]-cost[n];
for (auto [i,w]:nei[n])
{
if (i!=pa)
{
f=max(f,cost[i]+w);
getroot(i,n,root);
}
else
f=max(f,cost[root]-cost[n]);
}
cur.push_back({f,n});
}
void bfs(int n)
{
queue<pair<int,int>>Q;
Q.push({n,-1});
while (Q.size())
{
int n,m;
tie(n,m)=Q.front();
Q.pop();
for (auto [i,w]:nei[n])
{
if (i!=m)
{
cost[i]=cost[n]+w;
Q.push({i,n});
}
}
}
}
int travelTime(int n, int m, int l, int a[], int b[], int t[])
{
int f=0;
for (int i=0;i<m;i++)
{
f+=t[i];
nei[a[i]].push_back({b[i],t[i]});
nei[b[i]].push_back({a[i],t[i]});
}
for (int i=0;i<n;i++)
{
if (!vis[i])
{
heads.push_back(i);
dfs(i);
}
}
for (int i=0;i<n;i++)
vis[i]={};
vector<pair<int,int>>new_head;
for (auto i:heads)
{
getroot(i,-1,i);
sort(begin(cur),end(cur));
new_head.push_back(cur[0]);
cur={};
}
sort(begin(new_head),end(new_head));
reverse(begin(new_head),end(new_head));
for (int i=1;i<new_head.size();i++)
{
nei[new_head[0].second].push_back({new_head[i].second,l});
nei[new_head[i].second].push_back({new_head[0].second,l});
f+=l;
}
for (int i=0;i<n;i++)
cost[i]=0;
bfs(0);
w1=0;
new_head={};
for (int i=0;i<n;i++)
{
new_head.push_back({cost[i],i});
cost[i]=0;
}
sort(begin(new_head),end(new_head));
reverse(begin(new_head),end(new_head));
w1=1;
bfs(new_head[0].second);
new_head={};
for (int i=0;i<n;i++)
{
new_head.push_back({cost[i],i});
cost[i]=0;
}
sort(begin(new_head),end(new_head));
reverse(begin(new_head),end(new_head));
return new_head[0].first;
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:89:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int i=1;i<new_head.size();i++)
| ~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
15820 KB |
Output is correct |
2 |
Correct |
55 ms |
15564 KB |
Output is correct |
3 |
Correct |
38 ms |
13780 KB |
Output is correct |
4 |
Correct |
8 ms |
5468 KB |
Output is correct |
5 |
Correct |
6 ms |
4700 KB |
Output is correct |
6 |
Correct |
18 ms |
6652 KB |
Output is correct |
7 |
Correct |
1 ms |
3676 KB |
Output is correct |
8 |
Correct |
27 ms |
8792 KB |
Output is correct |
9 |
Correct |
35 ms |
11628 KB |
Output is correct |
10 |
Correct |
1 ms |
3676 KB |
Output is correct |
11 |
Correct |
48 ms |
12432 KB |
Output is correct |
12 |
Correct |
52 ms |
14368 KB |
Output is correct |
13 |
Correct |
2 ms |
3724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3676 KB |
Output is correct |
2 |
Correct |
1 ms |
3676 KB |
Output is correct |
3 |
Incorrect |
1 ms |
3676 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
15820 KB |
Output is correct |
2 |
Correct |
55 ms |
15564 KB |
Output is correct |
3 |
Correct |
38 ms |
13780 KB |
Output is correct |
4 |
Correct |
8 ms |
5468 KB |
Output is correct |
5 |
Correct |
6 ms |
4700 KB |
Output is correct |
6 |
Correct |
18 ms |
6652 KB |
Output is correct |
7 |
Correct |
1 ms |
3676 KB |
Output is correct |
8 |
Correct |
27 ms |
8792 KB |
Output is correct |
9 |
Correct |
35 ms |
11628 KB |
Output is correct |
10 |
Correct |
1 ms |
3676 KB |
Output is correct |
11 |
Correct |
48 ms |
12432 KB |
Output is correct |
12 |
Correct |
52 ms |
14368 KB |
Output is correct |
13 |
Correct |
2 ms |
3724 KB |
Output is correct |
14 |
Correct |
1 ms |
3676 KB |
Output is correct |
15 |
Correct |
1 ms |
3676 KB |
Output is correct |
16 |
Incorrect |
1 ms |
3676 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
9572 KB |
Output is correct |
2 |
Correct |
36 ms |
9720 KB |
Output is correct |
3 |
Correct |
35 ms |
9472 KB |
Output is correct |
4 |
Correct |
36 ms |
9720 KB |
Output is correct |
5 |
Correct |
36 ms |
9472 KB |
Output is correct |
6 |
Correct |
39 ms |
9564 KB |
Output is correct |
7 |
Correct |
37 ms |
9932 KB |
Output is correct |
8 |
Correct |
35 ms |
9508 KB |
Output is correct |
9 |
Correct |
35 ms |
9500 KB |
Output is correct |
10 |
Correct |
36 ms |
9700 KB |
Output is correct |
11 |
Correct |
1 ms |
3672 KB |
Output is correct |
12 |
Correct |
35 ms |
9552 KB |
Output is correct |
13 |
Correct |
34 ms |
9556 KB |
Output is correct |
14 |
Correct |
36 ms |
9556 KB |
Output is correct |
15 |
Correct |
38 ms |
9668 KB |
Output is correct |
16 |
Correct |
35 ms |
9424 KB |
Output is correct |
17 |
Correct |
33 ms |
9552 KB |
Output is correct |
18 |
Correct |
34 ms |
9556 KB |
Output is correct |
19 |
Correct |
36 ms |
9552 KB |
Output is correct |
20 |
Correct |
1 ms |
3676 KB |
Output is correct |
21 |
Correct |
1 ms |
3676 KB |
Output is correct |
22 |
Correct |
2 ms |
3676 KB |
Output is correct |
23 |
Correct |
34 ms |
9640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3676 KB |
Output is correct |
2 |
Correct |
1 ms |
3676 KB |
Output is correct |
3 |
Incorrect |
1 ms |
3676 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
15820 KB |
Output is correct |
2 |
Correct |
55 ms |
15564 KB |
Output is correct |
3 |
Correct |
38 ms |
13780 KB |
Output is correct |
4 |
Correct |
8 ms |
5468 KB |
Output is correct |
5 |
Correct |
6 ms |
4700 KB |
Output is correct |
6 |
Correct |
18 ms |
6652 KB |
Output is correct |
7 |
Correct |
1 ms |
3676 KB |
Output is correct |
8 |
Correct |
27 ms |
8792 KB |
Output is correct |
9 |
Correct |
35 ms |
11628 KB |
Output is correct |
10 |
Correct |
1 ms |
3676 KB |
Output is correct |
11 |
Correct |
48 ms |
12432 KB |
Output is correct |
12 |
Correct |
52 ms |
14368 KB |
Output is correct |
13 |
Correct |
2 ms |
3724 KB |
Output is correct |
14 |
Correct |
1 ms |
3676 KB |
Output is correct |
15 |
Correct |
1 ms |
3676 KB |
Output is correct |
16 |
Incorrect |
1 ms |
3676 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |