500B - New Year Permutation - CodeForces Solution


dfs and similar dsu graphs greedy math sortings *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define flush fflush(stdout)
#define ULL unsigned long long
#define LL long long
#define LD long double
#define EPS 0.000000001
#define MOD 1000000007
#define endl '\n'
#define ALL(x) x.begin(),x.end()
//#pragma GCC optimize("trapv")
#pragma GCC optimize("-Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-funroll-all-loops,-fpeel-loops,-funswitch-loops")


using namespace std;

short dx[] = { 1,-1,0,0 };
short dy[] = { 0,0,1,-1 };

LL FPow(int a, int b)
{
	if (b == 0)
		return 1;
	int res = FPow(a, b / 2);
	if (b % 2)
		return (res * res * a);
	else
		return (res * res);
}
vector<vector<int>> graph(305);
bool vis[305];
int32_t main() {
	//freopen("reduce.in", "r", stdin);
	//freopen("reduce.out", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin >> n;
	vector<int> permutation(n+1);
	for (int i = 1; i <= n; i++)
	{
		cin >> permutation[i];
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			char a;
			cin >> a;
			if (a == '1')
			{
				graph[i].push_back(j);
			}
		}
	}
	vector<pair<int, int>> ans;
	for (int i = 1; i <= n; i++)
	{
		if (!vis[i])
		{
			queue<int> q;
			vector<int> idxs;
			q.push(i);
			idxs.push_back(i);
			vis[i] = true;
			while (q.size())
			{
				int cur = q.front();
				for (int child = 0; child < graph[cur].size(); child++)
				{
					if (!vis[graph[cur][child]])
					{
						idxs.push_back(graph[cur][child]);
						q.push(graph[cur][child]);
						vis[graph[cur][child]] = true;
					}
				}
				q.pop();
			}
			sort(ALL(idxs));
			vector<int> temp;
			temp.push_back(-1);
			for (int idx = 0; idx < idxs.size(); idx++)
			{
				temp.push_back(permutation[idxs[idx]]);
			}
			sort(ALL(temp));
			int cnt = 1;
			for (int k = 0; k < idxs.size(); k++)
			{
				permutation[idxs[k]] = temp[cnt];
				cnt++;
			}
		}
	}
	for (int i = 1; i <= n; i++)
	{
		cout << permutation[i] << ' ';
	}
	
	return 0;
}


Comments

Submit
0 Comments
More Questions

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
Differences of the permutations
Doctor's Secret