Submission #696154

# Submission time Handle Problem Language Result Execution time Memory
696154 2023-02-05T16:07:59 Z amirhoseinfar1385 Valley (BOI19_valley) C++17
100 / 100
190 ms 64920 KB
    #include<bits/stdc++.h>
    using namespace std;
    struct yal{
    	long long u,v,w,bache;
    	long long getad(long long fu){
    		long long ret=(fu^u^v);
    		return ret;
    	}
    };
    long long inf=1e17;
    struct flca{
    	long long dis,wi,mina;
    	flca(){
    		mina=inf;
    		dis=0;
    		wi=0;
    	}
    };
    const long long maxn=100000+5;
    vector<yal>alled;
    vector<vector<long long>>adj;
    long long n,s,rishe,q;
    vector<long long>dis;
    pair<long long,long long>stf[maxn];
    long long para[maxn];
    flca lca[20][maxn];
    long long timea=0;
     
    void dfs(long long u,long long par=0,long long ind=0){
    	timea++;
    	stf[u].first=timea;
    	if(u!=rishe){
    		para[u]=ind;
    		alled[ind].bache=u;
    	}
    	for(auto x:adj[u]){
    		long long y=alled[x].getad(u);
    		if(y!=par){
    			dfs(y,u,x);
    			dis[u]=min(dis[u],dis[y]+alled[x].w);
    		}
    	}
    	stf[u].second=timea;
    }
     
    flca merge(flca a,flca b){
    	flca c;
    	c.dis=a.dis+b.dis;
    	c.wi=b.wi;
    	c.mina=min(a.mina,b.mina+a.dis);
    	return c;
    }
     
    void callca(){
    	for(long long i=0;i<20;i++){
    		for(long long j=1;j<=n;j++){
    			if(i==0){
    				if(j==rishe){
    					continue;
    				}
    				//if(i==0&&j==5){
    			//		cout<<" what "<<alled[para[j]].w<<"\n";
    			//	}
    				lca[i][j].dis=alled[para[j]].w;
    				lca[i][j].wi=alled[para[j]].getad(j);
    				lca[i][j].mina=alled[para[j]].w+dis[lca[i][j].wi];
    				continue;
    			}
    			lca[i][j]=merge(lca[i-1][j],lca[i-1][lca[i-1][j].wi]);	
    		}
    	}
    }
     
    void solve(long long ind,long long v){
    	long long u=alled[ind].bache;
    	if(stf[u].first<=stf[v].first&&stf[u].second>=stf[v].second){
    		flca now;
    		now.dis=0;
    		now.wi=v;
    		now.mina=dis[v];
    		for(long long i=19;i>=0;i--){
    			if(lca[i][v].wi==0){
    				continue;
    			}
    			if(stf[lca[i][v].wi].first<=stf[u].first&&stf[lca[i][v].wi].second>=stf[u].second){
    				continue;
    			}
    			now=merge(now,lca[i][v]);
    			v=now.wi;
    		}
    		//cout<<v<<" "<<u<<" asd \n";
    		if(u!=v){
    			now=merge(now,lca[0][v]);
    		}
    		if(now.mina==inf){
    			cout<<"oo\n";
    		}
    		else{
    			cout<<now.mina<<"\n";
    		}
    	}
    	else{
    		cout<<"escaped\n";
    	}
    }
     
     
    int main(){
    	ios::sync_with_stdio(0);
    	cin.tie(0);
    	cout.tie(0);
    	cin>>n>>s>>q>>rishe;
    	alled.resize(n-1);
    	adj.resize(n+1);
    	dis.resize(n+1,inf);
    	for(long long i=0;i<n-1;i++){
    		cin>>alled[i].u>>alled[i].v>>alled[i].w;
    		adj[alled[i].u].push_back(i);
    		adj[alled[i].v].push_back(i);
    	}
    	for(long long i=0;i<s;i++){
    		long long d;
    		cin>>d;
    		dis[d]=0;
    	}
    	dfs(rishe);
    	callca();
    	for(long long i=0;i<q;i++){
    		long long ind,u;
    		cin>>ind>>u;
    		ind--;
    		solve(ind,u);
    	}
    }
# Verdict Execution time Memory Grader output
1 Correct 20 ms 47300 KB Output is correct
2 Correct 20 ms 47316 KB Output is correct
3 Correct 22 ms 47304 KB Output is correct
4 Correct 20 ms 47336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 47300 KB Output is correct
2 Correct 20 ms 47316 KB Output is correct
3 Correct 22 ms 47304 KB Output is correct
4 Correct 20 ms 47336 KB Output is correct
5 Correct 22 ms 47316 KB Output is correct
6 Correct 19 ms 47316 KB Output is correct
7 Correct 19 ms 47408 KB Output is correct
8 Correct 20 ms 47328 KB Output is correct
9 Correct 19 ms 47352 KB Output is correct
10 Correct 19 ms 47444 KB Output is correct
11 Correct 19 ms 47384 KB Output is correct
12 Correct 19 ms 47404 KB Output is correct
13 Correct 20 ms 47384 KB Output is correct
14 Correct 19 ms 47444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 60472 KB Output is correct
2 Correct 140 ms 60100 KB Output is correct
3 Correct 146 ms 60032 KB Output is correct
4 Correct 190 ms 61792 KB Output is correct
5 Correct 151 ms 61900 KB Output is correct
6 Correct 181 ms 63760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 47300 KB Output is correct
2 Correct 20 ms 47316 KB Output is correct
3 Correct 22 ms 47304 KB Output is correct
4 Correct 20 ms 47336 KB Output is correct
5 Correct 22 ms 47316 KB Output is correct
6 Correct 19 ms 47316 KB Output is correct
7 Correct 19 ms 47408 KB Output is correct
8 Correct 20 ms 47328 KB Output is correct
9 Correct 19 ms 47352 KB Output is correct
10 Correct 19 ms 47444 KB Output is correct
11 Correct 19 ms 47384 KB Output is correct
12 Correct 19 ms 47404 KB Output is correct
13 Correct 20 ms 47384 KB Output is correct
14 Correct 19 ms 47444 KB Output is correct
15 Correct 128 ms 60472 KB Output is correct
16 Correct 140 ms 60100 KB Output is correct
17 Correct 146 ms 60032 KB Output is correct
18 Correct 190 ms 61792 KB Output is correct
19 Correct 151 ms 61900 KB Output is correct
20 Correct 181 ms 63760 KB Output is correct
21 Correct 121 ms 60536 KB Output is correct
22 Correct 126 ms 60108 KB Output is correct
23 Correct 140 ms 60088 KB Output is correct
24 Correct 158 ms 62068 KB Output is correct
25 Correct 160 ms 64920 KB Output is correct
26 Correct 120 ms 60552 KB Output is correct
27 Correct 147 ms 60216 KB Output is correct
28 Correct 137 ms 60032 KB Output is correct
29 Correct 157 ms 61516 KB Output is correct
30 Correct 183 ms 62860 KB Output is correct
31 Correct 124 ms 60528 KB Output is correct
32 Correct 135 ms 60284 KB Output is correct
33 Correct 142 ms 60216 KB Output is correct
34 Correct 168 ms 61964 KB Output is correct
35 Correct 156 ms 64712 KB Output is correct
36 Correct 147 ms 62668 KB Output is correct