1167C - News Distribution - CodeForces Solution


dfs and similar dsu graphs *1400

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))
K = int(next(toks))



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

for i in range(K):
    group_size = int(next(toks))

    if group_size == 0:
        continue
    if group_size == 1:
        next(toks)
        continue
    
    prev_person = None
    for _ in range(group_size):
                cur_person = int(next(toks))-1

                        if prev_person != None:
            adjacency_lists[prev_person].append(cur_person)
            adjacency_lists[cur_person].append(prev_person)
            
        prev_person = cur_person

comps = []
comp_of_vertex = [None for _ in range(N)]

for vertex in range(N):
            if comp_of_vertex[vertex] == None:
        comps.append([vertex])

                bfs_q = deque()
        bfs_q.append(vertex)
                        comp_of_vertex[vertex] = len(comps)-1

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

            for new_vertex in adjacency_lists[cur_vertex]:
                                                                                                if comp_of_vertex[new_vertex] == None:
                    comps[len(comps)-1].append(new_vertex)
                    comp_of_vertex[new_vertex] = len(comps)-1
                    bfs_q.append(new_vertex)
                    
for i in range(N):
    if i > 0:
        sys.stdout.write(" ")
    sys.stdout.write(str(len(comps[comp_of_vertex[i]])))
sys.stdout.write("\n")

 			 				    	  	 	 				      	

C++ Code:

#include<bits/stdc++.h>

using namespace std;

constexpr char ln = '\n';
constexpr int mod = 1e9 + 7;
constexpr long long MxN = 1e18;

int n, m, k;
string s, t;

void run_test_case(){
	cin >> n >> m;

	vector<vector<int>> nodes(n);
	for(int i = 0; i < m; i++){
		cin >> k;
		vector<int> group(k);
		for(int j = 0; j < k; j++)
			cin >> group[j], group[j]--;
		
		for(int j = 0; j + 1 < k; j++){
			nodes[group[j]].push_back(group[j + 1]);
			nodes[group[j + 1]].push_back(group[j]);
		}
	}
	// bfs
	vector<int> cost(n);
	vector<bool> visited(n);
	for(int i = 0; i < n; i++){
		if(visited[i])	continue;

		queue<int> que;
		que.push(i);
		vector<int> current;

		while(!que.empty()){
			int cur = que.front();
			que.pop();
			if(visited[cur])	continue;

			current.push_back(cur);
			visited[cur] = true;

			for(auto node : nodes[cur]){
				if(visited[node])	continue;

				que.push(node);
			}
		}
		for(auto cur : current)
			cost[cur] = current.size();
	}

	for(int i = 0; i < n; i++)
		cout << cost[i] << (i + 1 == n ? "" : " ");
}	

int main(){
	int test = 1;
	// cin >> test;
	while(test--){
		run_test_case();
	}
}
// 


Comments

Submit
0 Comments
More Questions

1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations