dfs and similar graphs *1700

Please click on ads to support us..

Python Code:

import threading
import sys

def main():
    n, m = map(int, input().split())

    graph = {}
    visited = [False for _ in range(n)]
    edges = []
    for _ in range(m):
        n1, n2 = map(int, input().split())
        graph[n1] = graph.get(n1, [])
        graph[n2] = graph.get(n2, [])
        graph[n1].append(n2)
        graph[n2].append(n1)
        edges.append((n1, n2))
    color = {1: 1}

    def coloring(node):
        visited[node - 1] = True
        for neighbor in graph[node]:
            if not visited[neighbor - 1]:
                color[neighbor] = 1 if color[node] == 0 else 0
                if not coloring(neighbor):
                    return False
            else:
                if color[neighbor] == color[node]:
                    return False
        return True

    if not coloring(1):
        print("NO")
    else:
        print("YES")
        binary = ""
        for n1, n2 in edges:
            if color[n1] == 1:
                binary += "0"
            else:
                binary += "1"
        print(binary)

if __name__ == '__main__':
    sys.setrecursionlimit(1 << 30)
    threading.stack_size(1 << 27)

    main_thread = threading.Thread(target=main)
    main_thread.start()
    main_thread.join()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int m, n;
int s[N], e[N];
int flag[N];
vector<int> vc[N];
bool dfs(int i, int f) {
	if (flag[i] == -1) flag[i] = f;
	if (flag[i] != f) return false; //成环,输出no 
	for (int j = 0; j < vc[i].size(); j++) { //深度搜索与i点相连的点 
		if (flag[vc[i][j]] == -1) {
			if (!dfs(vc[i][j], f ^ 1)) return false; //使f与1异或,保证每个点只作为一次起点 
		}
		else if (flag[vc[i][j]] == f) //成环,输出no 
			return false;
	}

	return true;
}
bool check()
{
	for (int i = 0; i < n; i++) {
		if (flag[i] == 0) {
			if (!dfs(i, 1))return 0;
		}
	}
	return 1;
}
int main() 
{
	cin >> m >> n;
	memset(flag, -1, sizeof(flag));
	for (int i = 0; i < n; i++) {
		cin >> s[i] >> e[i];
		vc[s[i]].push_back(e[i]);
		vc[e[i]].push_back(s[i]);
	}
	//check();
	//for (int i = 0; i < n; i++) {
	//	cout << color[i];
	//}
	//cout << endl;
	if (dfs(1,0)) {
		cout << "Yes" << endl;
		for (int i = 0; i < n; i++) {
			if (flag[s[i]] == 1)
				cout << flag[s[i]];
			else cout << 0;
		}	
		cout << endl;
	}
	else {
		cout << "NO" << endl;
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

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
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping