1805D - A Wide Wide Graph - CodeForces Solution


dfs and similar dp graphs trees

Please click on ads to support us..

Python Code:

n = int(input())
slovar = {}
for i in range(1, n + 1):
    slovar[i] = []
for i in range(n - 1):
    a, b = map(int, input().split())
    slovar[a].append(b)
    slovar[b].append(a)
from collections import deque

a = deque()
cash = set()
a.append(1)
cash.add(1)
while len(cash) != n:
    k = len(a)
    mass = []
    while k != 0:
        k -= 1
        for i in slovar[a.popleft()]:
            if i not in cash:
                cash.add(i)
                a.append(i)
                mass.append(i)
a = deque()
cash = set()
a.append(mass[0])
cash.add(mass[0])
z1 = mass[0]
s = 0
op = {}
while len(cash) != n:
    k = len(a)
    mass = []
    while k != 0:
        k -= 1
        x = a.popleft()
        for i in slovar[x]:
            if i not in cash:
                cash.add(i)
                a.append(i)
                mass.append(i)
                op[i] = x
    s += 1
z2 = mass[0]
i = z2
otvet = [z2]
while i != z1:
    otvet.append(op[i])
    i = op[i]
if s % 2 == 0:
    cash = set()
    main = otvet[s // 2]
    stack = deque()
    stack.append(main)
    cash.add(main)
    vivod = []
    for i in range(1, n + 1):
        if i <= s // 2:
            vivod.append(1)
        elif i > s:
            vivod.append(n)
        else:
            if i == s // 2 + 1:
                vivod.append(2)
            else:
                k = len(stack)
                while k != 0:
                    k -= 1
                    x = stack.popleft()
                    for i in slovar[x]:
                        if i not in cash:
                            cash.add(i)
                            stack.append(i)
                vivod.append(1 + len(cash))
    print(' '.join(str(i) for i in vivod))


else:
    cash2 = set()
    main1, main2 = otvet[s // 2], otvet[s // 2 + 1]
    stack = deque()
    stack.append(main1)
    cash2.add(main1)
    cash2.add(main2)
    stack.append(main2)
    vivod = []
    for i in range(1, n + 1):
        if i <= s // 2 + 1:
            vivod.append(1)
        elif i > s:
            vivod.append(n)
        else:
            if i == s // 2 + 2:
                vivod.append(3)
            else:
                k = len(stack)
                while k != 0:
                    k -= 1
                    x = stack.popleft()
                    for i in slovar[x]:
                        if i not in cash2:
                            cash2.add(i)
                            stack.append(i)
                vivod.append(1 + len(cash2))
    print(' '.join(str(i) for i in vivod))

C++ Code:

#include<bits/stdc++.h>
#define inf 1e18
#define mod 998244353
using namespace std;

int dis[100005];
int d[100005];
vector<int> g[100005];
int ans[100005];
int c=0;

void dfs(int u,int pa){
    for(auto v:g[u]){
        if(v==pa) continue;
        dis[v]=dis[u]+1;
        if(dis[v]>dis[c]) c=v;
        dfs(v,u);
    }
}

void solve(){
    int n;
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    dfs(1,0);
    dis[c]=0;
    dfs(c,0);
    for(int i=1;i<=n;i++){
        d[i]=max(d[i],max(dis[i],dis[c]-dis[i]));
    }
    dis[c]=0;
    dfs(c,0);
    for(int i=1;i<=n;i++){
        d[i]=max(d[i],max(dis[i],dis[c]-dis[i]));
        ans[d[i]]++;
    }
//    for(int i=1;i<=n;i++){
//        cout<<d[i]<<" ";
//    }
    int now=1;
    for(int i=0;i<n;i++){
        now+=ans[i];
        if(now<n) cout<<now<<" ";
        else cout<<n<<" ";
    }
    cout<<endl;
}


int main(){
//    ios::sync_with_stdio(false);
//    cin.tie(nullptr);
//        int n;
//        cin>>n;
//        while(n--){
            solve();
//        }
};


Comments

Submit
0 Comments
More Questions

780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack