1093D - Beautiful Graph - CodeForces Solution


dfs and similar graphs *1700

Please click on ads to support us..

Python Code:

import gc
import heapq
import itertools
import math
import sqlite3
from collections import Counter, deque, defaultdict
from sys import stdout
import time
from math import factorial, log, gcd
import sys
from decimal import Decimal
import threading
from heapq import *
from fractions import Fraction


def S():
    return sys.stdin.readline().split()


def I():
    return [int(i) for i in sys.stdin.readline().split()]


def II():
    return int(sys.stdin.readline())


def IS():
    return sys.stdin.readline().replace('\n', '')


def main():
    n, m = I()
    graph = [[] for _ in range(n)]
    for _ in range(m):
        u, v = I()
        graph[u - 1].append(v - 1)
        graph[v - 1].append(u - 1)
    visited = [-1] * n
    pool = set(range(n))
    ans = 1
    while pool:
        v = pool.pop()
        visited[v] = 0
        d = [1, 0]
        queue = deque([v])
        while queue:
            v = queue.pop()
            for u in graph[v]:
                if visited[u] == -1:
                    queue.append(u)
                    visited[u] = visited[v] ^ 1
                    d[visited[u]] += 1
                    pool.remove(u)
                elif visited[u] ^ visited[v] == 0:
                    print(0)
                    return
        ans = (ans * (pow(2, d[0], mod) + pow(2, d[1], mod))) % mod
    print(ans)


if __name__ == '__main__':
    mod = 998244353
    for _ in range(II()):
        main()
    

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const ll INF = 1e18;
const int inf = 1e9;
const int M = 3e5 + 10;
const int mod = 998244353;
ll ans=0;
int n,m,flag,cnt,tot,num,vis[M];
vector<int>e[M];
ll ksm(ll a,int p){ll ret=1;for(;p;p>>=1,a=a*a%mod)if(p&1)ret=ret*a%mod;return ret;}
void dfs(int u,int pos){
    if(!flag)return;num++;
    if(pos==1)vis[u]=2,tot++;
    else vis[u]=1;
    for(int i=0;i<e[u].size();i++){
        int v=e[u][i];
        if(vis[v]==vis[u]){
            flag=false;return;
        }
        if(vis[v])continue;
        dfs(v,(pos^1));
    }
}

void solve()
{
	cin>>n>>m;
	ans=1,cnt=0,flag=1;
	for(int i=1;i<=n;i++){
		vis[i]=0;e[i].clear();
	}
	for (int i = 1; i <=m; i++)
	{
		int u,v;cin>>u>>v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	for(int i=1;i<=n;i++){
		if(!vis[i]){
			num=tot=0;
			dfs(i,1);
			ans=ans*(ksm(2,tot)+ksm(2,num-tot))%mod;
		}
	}
	if(flag==0)ans=0;
	
	cout<<ans<<endl;
 return;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	cin >> t;
	while (t--){
		solve();
	}

	return 0;
}
  		 		        				 			 			 	


Comments

Submit
0 Comments
More Questions

72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)