This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "race.h"
#define lp(i,a,b) for(int i=a;i<b;i++)
#define ll int
#define pii pair<int,int>
#define ff first
#define ss second
#define mk make_pair
#define pb push_back
#define sz size()
#define mip map<int,int>::iterator
const int MAXN = 2e5+10 ;
using namespace std ;
int curTime ;
int fatSomaVal[MAXN] ,fatSomaKey[MAXN] , specialEdge[MAXN] ;
int ini[MAXN] , fim[MAXN] , qual[MAXN] , pai[MAXN] , resp[MAXN] ;
vector<pii> adj[MAXN] ;
map<int , int > mapa[MAXN] ;
bool marc[MAXN];
// ----------------- x -----------------
//BIT
int bit[MAXN] ;
void add(int x) { for(int i = x ; i < MAXN ; i+=(i&-i)) bit[i] ++ ; }
int qry(int x)
{
int tot = 0 ;
for(int i = x ; i > 0 ; i -= (i&-i))
tot += bit[i] ;
return tot ;
}
// ----------------- x -----------------
void dfs(int x)
{
ini[x] = ++curTime ;
for(pii i : adj[x])
if(ini[i.ff] == 0)
{
dfs(i.ff) ;
pai[i.ff] = x;
specialEdge[i.ff] = i.ss ;
}
fim[x] = curTime ;
}
int best_path(int N , int K , int H[][2] , int L[])
{
lp(i,0,MAXN) resp[i] = MAXN ;
lp(i,0,N-1)
{
int a = H[i][0] , b = H[i][1] ;
adj[a].pb(mk(b , L[i])) ;
adj[b].pb(mk(a,L[i])) ;
}
dfs(0) ;
queue<int> q ;
lp(i,0,N)
if(ini[i] == fim[i])
{
q.push(i) ;
marc[i] = true ;
qual[i] = q.sz ;
mapa[q.sz].insert(mk(0,0)) ;
}
while( !q.empty() )
{
int davez = q.front() ;
q.pop() ;
marc[davez] = true ;
add(ini[davez]) ;
if( !marc[pai[davez]] && (qry(fim[pai[davez]]) - qry(ini[pai[davez]])) == (fim[pai[davez]]-ini[pai[davez]]) )
{
marc[pai[davez]] = true ;
q.push(pai[davez]) ;
}
if( ini[davez] == fim[davez] ) continue ;
int bigChild = adj[davez][0].ff;
for(pii p : adj[davez])
if( mapa[qual[p.ff]].sz > mapa[qual[bigChild]].sz )
bigChild = p.ff ;
fatSomaKey[davez] = fatSomaKey[ bigChild ] + specialEdge[bigChild] ;
fatSomaVal[davez] = fatSomaVal[bigChild] + 1 ;
qual[davez] = qual[bigChild] ;
if( mapa[qual[davez]].find( K - fatSomaKey[davez] ) != mapa[qual[davez]].end() )
resp[davez] = mapa[qual[davez]][K-fatSomaKey[davez]] + fatSomaVal[davez] ;
int aux = specialEdge[bigChild] - fatSomaKey[davez] ;
if(mapa[qual[davez]].find(aux) == mapa[qual[davez]].end() )
mapa[qual[davez]].insert( mk(aux , 1 - fatSomaVal[davez] ) ) ;
mapa[qual[davez]][aux] = min( mapa[qual[davez]][aux] , 1 - fatSomaVal[davez] ) ;
lp(i,0,adj[davez].size())
{
int cur = adj[davez][i].ff ;
if(cur == bigChild) continue ;
vector<pii> atualiza ;
atualiza.pb( mk( specialEdge[cur] - fatSomaKey[davez] , 1 - fatSomaVal[davez] ) ) ;
for(mip it = mapa[qual[cur]].begin() ; it != mapa[qual[cur]].end() ; it++ )
{
int realX = it->ff + fatSomaKey[cur] + specialEdge[cur] ;
int realY = it->ss + fatSomaVal[cur] + 1 ;
int vaiUpdtarX = realX - fatSomaKey[davez] ;
int vaiUpdtarY = realY - fatSomaVal[davez] ;
int procuro = K - realX - fatSomaKey[davez] ;
atualiza.pb(mk(vaiUpdtarX, vaiUpdtarY)) ;
if( K == realX ) resp[davez] = min(resp[davez] , realY) ;
if( mapa[qual[davez]].find( procuro ) == mapa[qual[davez]].end() ) continue ;
resp[davez] = min(resp[davez], mapa[qual[davez]][procuro] + fatSomaVal[davez] + realY - 1 ) ;
}
for(pii p : atualiza)
{
if(mapa[qual[davez]].find(p.ff) == mapa[qual[davez]].end())
mapa[qual[davez]].insert(p) ;
mapa[qual[davez]][p.ff] = min( mapa[qual[davez]][p.ff] , p.ss ) ;
}
}
}
int ans = *min_element(resp,resp+N) ;
if(ans == MAXN) ans = -1 ;
return ans ;
}
Compilation message (stderr)
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:4:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define lp(i,a,b) for(int i=a;i<b;i++)
race.cpp:116:6:
lp(i,0,adj[davez].size())
~~~~~~~~~~~~~~~~~~~~~
race.cpp:116:3: note: in expansion of macro 'lp'
lp(i,0,adj[davez].size())
^~
# | 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... |