This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
4 200 3
2 3 1
3 4 2
1 2 9
*/
#include "citymapping.h"
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long,null_type,less_equal<long long>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("O2","unroll-loops","no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<ll,ll>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
inline ll mult(ll a,ll b){
if(a<0) a+=MOD;
if(b<0) b+=MOD;
if(a>=MOD) a%=MOD;
if(b>=MOD) b%=MOD;
return (a*b)%MOD;
}
inline ll mypow(ll a,ll b){
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1) res=mult(res,a);
a=mult(a,a);
b>>=1;
}
return res;
}
const int maxn=1e3+5;
ll dep[maxn];
int sz[maxn];
vector<pii> v[maxn];
bool vis[maxn];
int par[maxn];
int dfs(int u,int p){
int mx=0,vv=-1;
for(auto x:v[u]){
if(x.f!=p and !vis[x.f]){
if(sz[x.f]>mx){
mx=sz[x.f],vv=x.f;
}
}
}
if(vv==-1) return u;
return dfs(vv,u);
}
void connect(int a,int b){
//cout<<a<<' '<<b<<'\n';
par[b]=a;
v[a].pb({b,dep[b]-dep[a]});
sz[b]=1;
sz[1]++;
while(a!=1){
sz[a]++;
a=par[a];
}
}
void find_roads(int n, int q, int A[], int B[], int W[]) {
vector<pii> vv;
par[1]=-1;
for(int i=2;i<=n;i++){
dep[i]=get_distance(1,i);
vv.pb({dep[i],i});
}
sort(ALL(vv));
sz[1]=1;
REP(i,sz(vv)){
REP1(j,n) vis[j]=0;
int z=vv[i].s,rt=1;
while(true){
int p=dfs(rt,par[rt]);
if(p==rt){
connect(p,z);
break;
}
ll a=dep[p]-dep[rt],b=dep[z]-dep[rt],c=get_distance(z,p);
if(b==a+c){
connect(p,z);
break;
}
ll tot=(a+b-c)/2;
tot+=dep[rt];
while(dep[p]>tot) vis[p]=1,p=par[p];
rt=p;
}
}
int cnt=0;
REP1(i,n){
for(auto x:v[i]){
A[cnt]=i,B[cnt]=x.f,W[cnt]=x.s;
++cnt;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |