813C - The Tag Game - CodeForces Solution


dfs and similar graphs *1700

Please click on ads to support us..

Python Code:


import sys
from collections import deque

toks = (tok for tok in sys.stdin.read().split())


N = int(next(toks))
X = int(next(toks))-1

adj = [[] for _ in range(N)]

for _ in range(N-1):
        v1 = int(next(toks))-1
    v2 = int(next(toks))-1
    
        adj[v1].append(v2)
    adj[v2].append(v1)


    
def find_dsts(st):
    dsts = [None for _ in range(N)]

        bfs_q = deque()
    bfs_q.append(st)
        dsts[st] = 0

    while len(bfs_q) > 0:
        cur_v = bfs_q.popleft()

                for next_v in adj[cur_v]:
                                    if dsts[next_v] == None:
                                                                dsts[next_v] = dsts[cur_v]+1
                bfs_q.append(next_v)
                
    return dsts

dsts_from_alice = find_dsts(0)
dsts_from_bob = find_dsts(X)

answer = 2*dsts_from_alice[X]
vis = [False for _ in range(N)]

bfs_q = deque()
bfs_q.append(X)
vis[X] = True

while len(bfs_q) > 0:
    cur_v = bfs_q.popleft()

    for next_v in adj[cur_v]:
        if vis[next_v]: continue

                                        if dsts_from_alice[next_v] < dsts_from_bob[next_v]:
            answer = max(answer, 2*dsts_from_bob[next_v]-1)
                                        elif dsts_from_alice[next_v] == dsts_from_bob[next_v]:
            answer = max(answer, 2*dsts_from_bob[next_v])
                                                else:
            answer = max(answer, 2*dsts_from_alice[next_v])
                                    vis[next_v] = True
            bfs_q.append(next_v)

print(answer)
	   	  			 		 	     	 			  	  	

C++ Code:

#include<stdio.h>

#include<iostream>

#include<string.h>

using namespace std;

#define MAX 200005

int he[MAX],ne[MAX*2],etop=1,x,n,ans,d[MAX],d1[MAX];

bool vis[MAX];

struct edge

{

    int to,cost;

    edge(){}

    edge(int v,int c)

    {

        to=v;

        cost=c;

    }

}ed[MAX*2];



void dfs(int x)

{

    vis[x]=1;

    for(int i=he[x];i;i=ne[i])

    {

        edge& e=ed[i];

        if(vis[e.to])

            continue;

        vis[e.to]=1;

        d[e.to]=d[x]+e.cost;

        dfs(e.to);

    }

}



void dfs1(int x)

{

    vis[x]=1;

    for(int i=he[x];i;i=ne[i])

    {

        edge& e=ed[i];

        if(vis[e.to])

            continue;

        vis[e.to]=1;

        d1[e.to]=d1[x]+e.cost;

        dfs1(e.to);

    }

}



int main()

{

    scanf("%d%d",&n,&x);

    for(int i=1;i<=n-1;i++)

    {

        int u,v,c;

        scanf("%d%d",&u,&v);

        ed[etop]=edge(v,1);

        ne[etop]=he[u];

        he[u]=etop++;

        ed[etop]=edge(u,1);

        ne[etop]=he[v];

        he[v]=etop++;

    }

    dfs(x);

    memset(vis,0,sizeof(vis));

    dfs1(1);

    ans=0;

    for(int i=1;i<=n;i++)

    {

        //cout<<"d["<<i<<"]"<<d[i]<<"  d1["<<i<<"]"<<d1[i]<<endl;

        if(d[i]>=d1[i])

            continue;

        ans=max(ans,d1[i]);

    }

    cout<<ans*2<<endl;

    return 0;

}


Comments

Submit
0 Comments
More Questions

1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters