1726D - Edge Split - CodeForces Solution


constructive algorithms dfs and similar graphs probabilities trees

Please click on ads to support us..

Python Code:

import sys
input = sys.stdin.buffer.readline 


def find_root(root_dict, x):
    L = []
    while x != root_dict[x]:
        L.append(x)
        x = root_dict[x]
    for y in L:
        root_dict[y] = x
    return x

def process(n, G):
    m = len(G)
    answer = [None for i in range(m)]
    root_dict = [i for i in range(n+1)]
    good_edges = []
    bad_edges = []
    for i in range(m):
        u, v = G[i]
        u1 = find_root(root_dict, u)
        v1 = find_root(root_dict, v)
        if u1 != v1:
            good_edges.append([u, v, i])
            root_dict[u1] = v1
            answer[i] = '0'
        else:
            bad_edges.append([u, v, i])
            answer[i] = '1'
    if len(bad_edges)==3:
        u1, v1, i1 = bad_edges[0]
        u2, v2, i2 = bad_edges[1]
        u3, v3, i3 = bad_edges[2]
        L = [u1, v1, u2, v2, u3, v3]
        L = sorted(L)
        if L[0]==L[1] and L[2]==L[3] and L[4]==L[5]:
            answer[i3] = '0'
            root_dict = [i for i in range(n+1)]
            root_dict[u3] = v3
            for u, v, i in good_edges:
                u1 = find_root(root_dict, u)
                v1 = find_root(root_dict, v)
                if u1==v1:
                    answer[i] = '1'
                    break
                else:
                    root_dict[u1] = v1
    answer = ''.join(answer)
    sys.stdout.write(f'{answer}\n')
    return

t = int(input())
for i in range(t):
    n, m = [int(x) for x in input().split()]
    G = []
    for j in range(m):
        u, v = [int(x) for x in input().split()]
        G.append([u, v])
    process(n, G)

C++ Code:

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define endl '\n'
#define int long long
#define lowbit(n) (n&(-n))
#define db long double
#define pdd pair<double, double>
#define pii pair<int, int>
#define x first
#define y second
#define ll long long
const int N = 2e5 + 10, mod = 998244353;
int n, m, x, p[N];
pii a[N];
int find(int x) {
	if (x != p[x])p[x] = find(p[x]);
	return p[x];
}
void solve() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)p[i] = i;
	set<int> se;
	pii tem;
	string s(m, '0');
	for (int i = 0; i < m; i++) {
		int u, v;
		cin >> u >> v;
		a[i] = { u,v };
		int fu = find(u), fv = find(v);
		if (fu == fv)s[i] = '1', se.insert(u), se.insert(v), tem = a[i];
		else p[fu] = p[fv];
	}
	if (se.size() == 3 && m == n + 2) {
		for (int i = 0; i < m; i++)
			if (a[i] == tem) {
				s[i] = '0';
				for (int j = 0; j < m; j++) {
					if (i != j && (tem.x == a[j].x || tem.x == a[j].y))
						s[j] = '1';
				}
			}
	}
	cout << s << endl;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int T = 1; cin >> T;
	while (T--) solve();
}


Comments

Submit
0 Comments
More Questions

1665D - GCD Guess
29A - Spit Problem
1097B - Petr and a Combination Lock
92A - Chips
1665B - Array Cloning Technique
1665A - GCD vs LCM
118D - Caesar's Legions
1598A - Computer Game
1605A - AM Deviation
1461A - String Generation
1585B - Array Eversion
1661C - Water the Trees
1459A - Red-Blue Shuffle
1661B - Getting Zero
1661A - Array Balancing
1649B - Game of Ball Passing
572A - Arrays
1455A - Strange Functions
1566B - MIN-MEX Cut
678C - Joty and Chocolate
1352E - Special Elements
1520E - Arranging The Sheep
1157E - Minimum Array
1661D - Progressions Covering
262A - Roma and Lucky Numbers
1634B - Fortune Telling
1358A - Park Lighting
253C - Text Editor
365B - The Fibonacci Segment
75A - Life Without Zeros