#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ll long long
#define INF 1000000
#define MOD 1000000007
#define pb push_back
#define ve vector<ll>
#define dos pair<ll,ll>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
struct greateri
{
template<class T>
bool operator()(T const &a, T const &b) const { return a > b; }
};
int n;
int dp[100001],dp1[100001];
vector<pair<int,int>>tr[100001];
bool vis[100001];
map<dos,int>ga;
int solve(int node,int p)
{
////debug(node);
int fat=0;
vis[node]=1;
for(auto it:tr[node])
{
if(it.first!=p)
{
fat=max(fat,it.second+solve(it.first,node));
}
}
return dp[node]=fat;
}
int sol=0;
int ans;
set<int>kal;
void solve1(int node,int p,int besti)
{
//debug(node);
dp1[node]=max(dp[node],besti);
ans=min(dp1[node],ans);
set<pair<int,int>>gg,gg1;
for(auto it:tr[node])
{
if(it.first!=p)
{
gg.insert({-(dp[it.first]+it.second),it.first});
}
}
gg1=gg;
for(auto it:gg)
{
int p1=besti;
int p2=0;
auto u=it;
gg1.erase(it);
if(gg1.size()>0)
{
auto k =(*gg1.begin());
p2=-k.first;
}
////debug(u.second);
if(u.second>=0 && u.second<n && u.second!=p)solve1(u.second,node,max(p1,p2)+ga[{min(node,u.second),max(node,u.second)}]);
gg1.insert(u);
}
return;
}
int besti=0;
int far=0;
int nodi=0;
void dfs(int node,int p)
{
vis[node]=1;
if(far>besti)
{
besti=far;
nodi=node;
}
for (auto it:tr[node])
{
if(it.first!=p)
{
far+=it.second;
dfs(it.first,node);
far-=it.second;
}
}
}
void dfs1(int node,int p)
{
if(far>besti)
{
besti=far;
nodi=node;
}
for (auto it:tr[node])
{
if(it.first!=p)
{
far+=it.second;
dfs1(it.first,node);
far-=it.second;
}
}
}
int diameter(int root)
{
besti=0;
dfs(root,-1);
besti=0;
dfs1(nodi,-1);
return besti;
}
int step()
{
int ans=0;
memset(vis,0,sizeof vis);
for (int i = 0; i < n; ++i)
{
if(!vis[i])
{
ans=max(ans,diameter(i));
}
}
return ans;
}
int travelTime(int N,int M,int L,int A[],int B[],int T[])
{
//flash;
n=N;
////debug(n);
ans=pow(10,9);
for (int i = 0; i < M; ++i)
{
int x=A[i];
int y=B[i];
int z=T[i];
//debugs(x,y);
tr[x].pb({y,z});
tr[y].pb({x,z});
ga[{min(x,y),max(x,y)}]=z;
}
sol=max(step(),sol);
memset(vis,0,sizeof vis);
for (int i = 0; i < n; ++i)
{
if(!vis[i])
{
solve(i,-1);
solve1(i,-1,0);
kal.insert(-ans);
ans=pow(10,9);
}
}
if(kal.size()>1)
{
auto u=(*kal.begin());
kal.erase(u);
auto k=(*kal.begin());
sol=max(-u+L+-k,sol);
}
if(kal.size()>1)
{
auto u=(*kal.begin());
kal.erase(u);
auto k=(*kal.begin());
sol=max(-u+L*2+-k,sol);
}
return sol;
}
//code the AC sol !
// BS/queue/map
Compilation message
dreaming.cpp:5:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("O3")
dreaming.cpp:6:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("unroll-loops")
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
28028 KB |
Output is correct |
2 |
Correct |
187 ms |
28284 KB |
Output is correct |
3 |
Correct |
115 ms |
25848 KB |
Output is correct |
4 |
Correct |
23 ms |
6648 KB |
Output is correct |
5 |
Correct |
19 ms |
4856 KB |
Output is correct |
6 |
Correct |
36 ms |
8696 KB |
Output is correct |
7 |
Correct |
7 ms |
2812 KB |
Output is correct |
8 |
Correct |
90 ms |
13688 KB |
Output is correct |
9 |
Correct |
112 ms |
20692 KB |
Output is correct |
10 |
Correct |
7 ms |
3064 KB |
Output is correct |
11 |
Correct |
187 ms |
21752 KB |
Output is correct |
12 |
Correct |
215 ms |
25336 KB |
Output is correct |
13 |
Correct |
7 ms |
3064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
28028 KB |
Output is correct |
2 |
Correct |
187 ms |
28284 KB |
Output is correct |
3 |
Correct |
115 ms |
25848 KB |
Output is correct |
4 |
Correct |
23 ms |
6648 KB |
Output is correct |
5 |
Correct |
19 ms |
4856 KB |
Output is correct |
6 |
Correct |
36 ms |
8696 KB |
Output is correct |
7 |
Correct |
7 ms |
2812 KB |
Output is correct |
8 |
Correct |
90 ms |
13688 KB |
Output is correct |
9 |
Correct |
112 ms |
20692 KB |
Output is correct |
10 |
Correct |
7 ms |
3064 KB |
Output is correct |
11 |
Correct |
187 ms |
21752 KB |
Output is correct |
12 |
Correct |
215 ms |
25336 KB |
Output is correct |
13 |
Correct |
7 ms |
3064 KB |
Output is correct |
14 |
Correct |
6 ms |
2808 KB |
Output is correct |
15 |
Correct |
7 ms |
2724 KB |
Output is correct |
16 |
Correct |
6 ms |
2808 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
7 ms |
2808 KB |
Output is correct |
19 |
Correct |
6 ms |
2808 KB |
Output is correct |
20 |
Correct |
6 ms |
2808 KB |
Output is correct |
21 |
Correct |
6 ms |
2808 KB |
Output is correct |
22 |
Correct |
6 ms |
2808 KB |
Output is correct |
23 |
Correct |
6 ms |
2808 KB |
Output is correct |
24 |
Correct |
6 ms |
2812 KB |
Output is correct |
25 |
Correct |
6 ms |
2808 KB |
Output is correct |
26 |
Correct |
6 ms |
2808 KB |
Output is correct |
27 |
Correct |
6 ms |
2812 KB |
Output is correct |
28 |
Incorrect |
6 ms |
2808 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
28028 KB |
Output is correct |
2 |
Correct |
187 ms |
28284 KB |
Output is correct |
3 |
Correct |
115 ms |
25848 KB |
Output is correct |
4 |
Correct |
23 ms |
6648 KB |
Output is correct |
5 |
Correct |
19 ms |
4856 KB |
Output is correct |
6 |
Correct |
36 ms |
8696 KB |
Output is correct |
7 |
Correct |
7 ms |
2812 KB |
Output is correct |
8 |
Correct |
90 ms |
13688 KB |
Output is correct |
9 |
Correct |
112 ms |
20692 KB |
Output is correct |
10 |
Correct |
7 ms |
3064 KB |
Output is correct |
11 |
Correct |
187 ms |
21752 KB |
Output is correct |
12 |
Correct |
215 ms |
25336 KB |
Output is correct |
13 |
Correct |
7 ms |
3064 KB |
Output is correct |
14 |
Correct |
6 ms |
2808 KB |
Output is correct |
15 |
Correct |
7 ms |
2724 KB |
Output is correct |
16 |
Correct |
6 ms |
2808 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
7 ms |
2808 KB |
Output is correct |
19 |
Correct |
6 ms |
2808 KB |
Output is correct |
20 |
Correct |
6 ms |
2808 KB |
Output is correct |
21 |
Correct |
6 ms |
2808 KB |
Output is correct |
22 |
Correct |
6 ms |
2808 KB |
Output is correct |
23 |
Correct |
6 ms |
2808 KB |
Output is correct |
24 |
Correct |
6 ms |
2812 KB |
Output is correct |
25 |
Correct |
6 ms |
2808 KB |
Output is correct |
26 |
Correct |
6 ms |
2808 KB |
Output is correct |
27 |
Correct |
6 ms |
2812 KB |
Output is correct |
28 |
Incorrect |
6 ms |
2808 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
7800 KB |
Output is correct |
2 |
Correct |
55 ms |
8184 KB |
Output is correct |
3 |
Correct |
59 ms |
8184 KB |
Output is correct |
4 |
Correct |
55 ms |
8184 KB |
Output is correct |
5 |
Correct |
55 ms |
8184 KB |
Output is correct |
6 |
Correct |
58 ms |
8568 KB |
Output is correct |
7 |
Correct |
59 ms |
8440 KB |
Output is correct |
8 |
Correct |
55 ms |
8056 KB |
Output is correct |
9 |
Correct |
55 ms |
8060 KB |
Output is correct |
10 |
Correct |
59 ms |
8440 KB |
Output is correct |
11 |
Correct |
6 ms |
2808 KB |
Output is correct |
12 |
Correct |
11 ms |
3576 KB |
Output is correct |
13 |
Correct |
12 ms |
3704 KB |
Output is correct |
14 |
Correct |
11 ms |
3576 KB |
Output is correct |
15 |
Correct |
11 ms |
3576 KB |
Output is correct |
16 |
Incorrect |
11 ms |
3576 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
28028 KB |
Output is correct |
2 |
Correct |
187 ms |
28284 KB |
Output is correct |
3 |
Correct |
115 ms |
25848 KB |
Output is correct |
4 |
Correct |
23 ms |
6648 KB |
Output is correct |
5 |
Correct |
19 ms |
4856 KB |
Output is correct |
6 |
Correct |
36 ms |
8696 KB |
Output is correct |
7 |
Correct |
7 ms |
2812 KB |
Output is correct |
8 |
Correct |
90 ms |
13688 KB |
Output is correct |
9 |
Correct |
112 ms |
20692 KB |
Output is correct |
10 |
Correct |
7 ms |
3064 KB |
Output is correct |
11 |
Correct |
187 ms |
21752 KB |
Output is correct |
12 |
Correct |
215 ms |
25336 KB |
Output is correct |
13 |
Correct |
7 ms |
3064 KB |
Output is correct |
14 |
Correct |
7 ms |
2936 KB |
Output is correct |
15 |
Correct |
8 ms |
3064 KB |
Output is correct |
16 |
Correct |
9 ms |
3192 KB |
Output is correct |
17 |
Incorrect |
7 ms |
2936 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
28028 KB |
Output is correct |
2 |
Correct |
187 ms |
28284 KB |
Output is correct |
3 |
Correct |
115 ms |
25848 KB |
Output is correct |
4 |
Correct |
23 ms |
6648 KB |
Output is correct |
5 |
Correct |
19 ms |
4856 KB |
Output is correct |
6 |
Correct |
36 ms |
8696 KB |
Output is correct |
7 |
Correct |
7 ms |
2812 KB |
Output is correct |
8 |
Correct |
90 ms |
13688 KB |
Output is correct |
9 |
Correct |
112 ms |
20692 KB |
Output is correct |
10 |
Correct |
7 ms |
3064 KB |
Output is correct |
11 |
Correct |
187 ms |
21752 KB |
Output is correct |
12 |
Correct |
215 ms |
25336 KB |
Output is correct |
13 |
Correct |
7 ms |
3064 KB |
Output is correct |
14 |
Correct |
6 ms |
2808 KB |
Output is correct |
15 |
Correct |
7 ms |
2724 KB |
Output is correct |
16 |
Correct |
6 ms |
2808 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
7 ms |
2808 KB |
Output is correct |
19 |
Correct |
6 ms |
2808 KB |
Output is correct |
20 |
Correct |
6 ms |
2808 KB |
Output is correct |
21 |
Correct |
6 ms |
2808 KB |
Output is correct |
22 |
Correct |
6 ms |
2808 KB |
Output is correct |
23 |
Correct |
6 ms |
2808 KB |
Output is correct |
24 |
Correct |
6 ms |
2812 KB |
Output is correct |
25 |
Correct |
6 ms |
2808 KB |
Output is correct |
26 |
Correct |
6 ms |
2808 KB |
Output is correct |
27 |
Correct |
6 ms |
2812 KB |
Output is correct |
28 |
Incorrect |
6 ms |
2808 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |