357B - Flag Day - CodeForces Solution


constructive algorithms implementation *1400

Please click on ads to support us..

Python Code:

n,m = map(int,input().split())

group = [list(map(int,input().split())) for _ in range(m)]

colours = [0]*(n+1)
visited = [False]*(n+1)

colours[group[0][0]]=1
colours[group[0][1]]=2
colours[group[0][2]]=3

visited[group[0][0]]=visited[group[0][1]]=visited[group[0][2]]=True

for i in range(1,m):
    curr = group[i]
    cset = set()
    for j in curr:
        if visited[j]:
            cset.add(colours[j])

    for j in curr:
        if visited[j]:
            continue
        
        if 1 not in cset:
            colours[j]=1
            cset.add(1)
        elif 2 not in cset:
            colours[j]=2
            cset.add(2)
        else:
            colours[j]=3
            cset.add(3)
        visited[j]=True

print(*colours[1:])

    


C++ Code:

#include <bits/stdc++.h>
using namespace std;
// typedef
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef vector<pii> vpii;
typedef deque<int> di;
typedef deque<ll> dll;
// define instruction
#define double long double
#define rep(i, x, y) for (int i = x; i < y; i++)
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define all(x) begin(x), end(x)
// define constants
#define MOD 1000000007
#define inf 1e18
#define PI 3.141592653589793238462
// defined functions
ll setBitNumber(ll n)
{
	if (n == 0)
		return 0;
	ll msb = 0;
	n = n / 2;
	while (n != 0)
	{
		n = n / 2;
		msb++;
	}
	return (1 << msb);
}
ll countBits(ll number)
{ // log function in base  take only integer part
	return (ll)log2(number) + 1;
}
ll mul(ll a, ll b, ll mod = 1000000007)
{
	return ((a % mod) * (b % mod)) % mod;
}
ll gcd(ll a, ll b)
{
	if (b == 0)
	{
		return a;
	}
	return gcd(b, a % b);
}
bool isPrime(ll n)
{
	if (n <= 1)
		return false;
	if (n == 2 || n == 3)
		return true;
	if (n % 2 == 0 || n % 3 == 0)
		return false;

	for (ll i = 5; i <= sqrt(n); i = i + 6)
		if (n % i == 0 || n % (i + 2) == 0)
			return false;
	return true;
}
ll expo(ll a, ll b, ll mod)
{
	ll res = 1;
	while (b > 0)
	{
		if (b & 1)
			res = (res * a) % mod;
		a = (a * a) % mod;
		b = b >> 1;
	}
	return res;
}
int countDigit(ll n)
{
	return floor(log10(n) + 1);
}
ll lcm(ll a, ll b)
{
	return (a / gcd(a, b)) * b;
}
ll countDivisors(ll n)
{
	ll cnt = 0;
	for (ll i = 1; i <= sqrt(n); i++)
	{
		if (n % i == 0 && isPrime(i))
		{
			if (n / i == i)
				cnt++;
			else // Otherwise count both
				cnt = cnt + 2;
		}
	}
	return cnt;
}

int fact(int n);

int nCr(int n, int r)
{
	return fact(n) / (fact(r) * fact(n - r));
}

// Returns factorial of n
int fact(int n)
{
	if (n == 0)
		return 1;
	int res = 1;
	for (int i = 2; i <= n; i++)
		res = res * i;
	return res;
}

// main solution
//@aniketrajput25
void solve()
{
	int n, m;
	cin >> n >> m;
	map<int, int> mp;
	int a, b, c;
	cin >> a >> b >> c;
	mp[a] = 1;
	mp[b] = 2;
	mp[c] = 3;
	for (int i = 0; i < m - 1; i++)
	{
		vi arr(3);
		for (int j = 0; j < 3; j++)
			cin >> arr[j];
		bool f1 = true, f2 = true, f3 = true;

		for (auto x : arr)
		{
			if (mp.count(x) == 1)
			{
				if (mp[x] == 1)
				{
					f1 = false;
				}
				else if (mp[x] == 2)
					f2 = false;
				else if (mp[x] == 3)
					f3 = false;
			}
		}
		for (auto x : arr)
		{
			if (mp.count(x) == 0)
			{
				if (f1)
				{
					mp[x] = 1;
					f1 = false;
				}
				else if (f2)
				{
					mp[x] = 2;
					f2 = false;
				}
				else if (f3)
				{
					mp[x] = 3;
					f3 = false;
				}
			}
		}
	}
	for (int i=1;i<=n;i++)
		cout << mp[i] << " ";
}
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t = 1;
	// cin >> t;
	while (t--)
	{
		solve();
	}

	return 0;
}
	     			 			   		   	   	  	 	


Comments

Submit
0 Comments
More Questions

17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers