dfs and similar graphs greedy trees *1600

Please click on ads to support us..

Python Code:

import  threading
import sys
from sys import stdin
input=stdin.readline
sys.setrecursionlimit(10**8)
from collections import  defaultdict
def main():
    n=int(input())
    graph=[[] for _ in range(n)]
    degree=[0]*(n)

    for _  in range(n-1):
        a,b=map(int,input().split())
        a-=1
        b-=1
        degree[a]+=1
        degree[b]+=1
        graph[a].append(b)
        graph[b].append(a)
    start=max(degree)
    for i,el in enumerate(degree):
        if el==start:
            start=i
            break
    res=[0]*(n)
    def solve(node,p1):
        curr=1
        for nie in graph[node]:
            if nie !=p1:
                while res[node]==curr or res[p1]==curr:
                    curr+=1
                res[nie]=curr
                solve(nie,node)
                curr+=1
    res[start]=1
    solve(start,start)
    print(len(set(res)))
    print(*res)
 
threading.stack_size(10 ** 8)
t = threading.Thread(target=main)
t.start()
t.join()

C++ Code:

#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <stack>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
using namespace std;
const int mod=1e9+7;
#define ll long long int

void addEdge(vector<int> adj[], int u, int v)
{
	adj[u].push_back(v);
	adj[v].push_back(u);
}

void BFS_root(vector<int> adj[], vector<int> &visited , vector<int>&parent, vector<int>&dist, int V, int s,vector<int>&col){
    queue<int> bfs; bfs.push(s); dist[s]=0; visited[s]=1; col[1]=1;
    while (!bfs.empty()) {
        int u = bfs.front();
        bfs.pop(); int ct=1;
        for(int i=0;i<adj[u].size();i++){
            if(visited[adj[u][i]]==-1){
                visited[adj[u][i]]=1; parent[adj[u][i]]=u; dist[adj[u][i]]=dist[u]+1;int x=adj[u][i];
                bfs.push(adj[u][i]);
                // cout << adj[u][i] << " pushed\n";
                // print_queue(bfs);
                if(ct!=col[u] && u!=1 && ct!=col[parent[u]]) col[x]=ct++;
                else if(ct!=col[u] && u==1) col[x]=ct++;
                else if(ct==col[u] && u==1) {col[x]=ct+1;ct+=2;}
                else if(ct==col[u] && u!=1 && ct==col[parent[u]]) {col[x]=ct+1;ct+=2;}
                else if(u!=1){
                    if(ct==col[u] && ct+1==col[parent[u]]) {col[x]=ct+2;ct=col[x]+1;}
                    else if(ct+1==col[u] && ct==col[parent[u]]) {col[x]=ct+2;ct=col[x]+1;}
                    else {col[x]=ct+1;ct=col[x]+1;}
                }
            }
        }
    }
}

void BFS_main(vector<int> adj[], vector<int> &visited , vector<int>&parent, vector<int>&dist, int V,vector<int>&col){
    for(int i=0;i<V;i++){visited.push_back(-1);}
    for(int i=0;i<V;i++){parent.push_back(-1);}
    for(int i=0;i<V;i++){dist.push_back(-1);}
    BFS_root(adj,visited ,parent,dist,V,1,col);
    // for(int i=0;i<V;i++) {
    //     if(visited[i]==-1) {BFS_root(adj,visited ,parent,dist,V,i);}
    // }
}

int main(){
    int n;cin>>n; vector<int>adj[n+1];
    for(int i=0;i<n-1;i++){
        int a,b; cin >>a>>b; addEdge(adj,a,b);
    }
    vector<int> visited; vector<int> parent; vector<int> dist; vector<int>col(n+1);
    BFS_main(adj,visited,parent,dist,n+1,col);
    col[0]=-1;int sm=0;
    for(int i=1;i<=n;i++) sm=max(sm,col[i]);
    cout << sm << endl;
    for(int i=1;i<=n;i++) cout<<col[i]<< " ";cout<<endl;
}


Comments

Submit
0 Comments
More Questions

337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation