1738F - Connectivity Addicts - CodeForces Solution


constructive algorithms dsu graphs greedy interactive shortest paths trees *2400

Please click on ads to support us..

Python Code:

from sys import stdout

t = int(input())
for tidx in range(t):
    n = int(input())
    d = [int(x) for x in input().split()]
    f = [(d[i], i+1) for i in range(n)]
    f.sort()
    fidx = n-1
    colidx = 0 
    color = [-1]*n
    finalcol = list(range(1,n+1))
    while fidx >= 0:
        dx = f[fidx][0]
        q = f[fidx][1]
        color[q-1] = colidx
        for i in range(dx):
            print("?",q)
            stdout.flush()
            e = int(input())
            if color[e-1] != -1:
                finalcol[colidx] = finalcol[color[e-1]]
                break
            else: color[e-1] = colidx
        while fidx >= 0 and color[f[fidx][1]-1] != -1:
            fidx -=1
        colidx += 1
    print("!", *[finalcol[color[i]] for i in range(n)])
    stdout.flush()
    
            
            

C++ Code:

#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
const int C=1005;

bool checked[C];
int conn[C];
void dfs(int root, vector <vector <int>> &gr, int color){
	for (auto &x: gr[root]){
		if (checked[x]) continue;
		checked[x] = true;
		conn[x] = color;
		dfs(x, gr, color);
	}
}

void mark_conns(vector <vector <int>> &gr, int n){
	int color = 1;
	for (int i=1; i<=n; i++) checked[i] = false;
	for (int i=1; i<=n; i++) conn[i] = -1;

	for (int i=1; i<=n; i++){
		if (checked[i]) continue;
		conn[i] = color;
		dfs(i, gr, color);
		color++;
	}
}

int query(int x){
	printf ("? %d\n", x);
	fflush(stdout);
	int res;
	scanf ("%d", &res);
	return res;
}

bool visited[C];
void solve_single_case(){
	vector <std::pair<int,int>> degrees;
	vector <vector <int>> gr(C);

	int n; scanf ("%d", &n);
	for (int i=1; i<=n; i++){
	       	int deg; scanf ("%d", &deg);
		degrees.push_back({-deg, i});
	}
	std::sort(degrees.begin(), degrees.end());

	for (auto &x: degrees){
		if (visited[x.second]) continue;
		visited[x.second] = true;
		for (int i=0; i<-x.first; i++){
			int vertex = query(x.second);
			gr[x.second].push_back(vertex);
			gr[vertex].push_back(x.second);

			if (visited[vertex]) break;
			visited[vertex] = true;
		}
	}

	mark_conns(gr, n);
	printf ("! ");
	for (int i=1; i<=n; i++) printf ("%d ", conn[i]);
	printf ("\n");
	fflush(stdout);

	for (int i=1; i<=n; i++) visited[i] = false;
}

int main(){
	int t; scanf ("%d", &t);
	while(t--) solve_single_case();
return 0;}


Comments

Submit
0 Comments
More Questions

1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles