0% found this document useful (0 votes)
17 views

codeforces solution find the permutation

The document contains a C++ program that computes combinations and handles permutations based on a given adjacency matrix. It includes functions for modular arithmetic, factorial precomputation, and sorting based on connection counts. The main function processes multiple test cases, reading input and outputting the sorted permutation of indices for each case.

Uploaded by

Amritansha Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

codeforces solution find the permutation

The document contains a C++ program that computes combinations and handles permutations based on a given adjacency matrix. It includes functions for modular arithmetic, factorial precomputation, and sorting based on connection counts. The main function processes multiple test cases, reading input and outputting the sorted permutation of indices for each case.

Uploaded by

Amritansha Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <bits/stdc++.

h>

using namespace std;

#define ll long long

const int MOD=1e9+7;

vector<ll> factorial, inverse;

long long binpow(long long a, long long b) {

long long res = 1;

while (b > 0) {

if (b & 1) {

res = res * a;

a = a * a;

b >>= 1;

return res;

ll power(ll base, ll exp) {

ll res = 1;

while (exp > 0) {

if (exp % 2 == 1) {

res = (res * base) % MOD;

base = (base * base) % MOD;

exp /= 2;

return res;

void precompute(int n) {

factorial.resize(n + 1);

inverse.resize(n + 1);

factorial[0] = 1;
for (int i = 1; i <= n; i++) {

factorial[i] = (factorial[i - 1] * i) % MOD;

inverse[n] = power(factorial[n], MOD - 2);

for (int i = n - 1; i >= 0; i--) {

inverse[i] = (inverse[i + 1] * (i + 1)) % MOD;

ll nCr(int n, int r) {

if (r > n || r < 0) {

return 0;

return (factorial[n] * inverse[r] % MOD * inverse[n - r] % MOD) % MOD;

void solve(){

int n;

cin>>n;

vector<string> g(n);

for (int i=0;i<n;i++){

cin>>g[i];

vector<int> perm(n);

vector<pair<int, int>> pairs(n);

for (int i = 0; i < n; ++i) {

pairs[i] = {i, 0};

for (int i = 0; i < n; ++i) {

for (int j = i + 1; j < n; ++j) {

if (g[i][j] == '1') {

++pairs[i].second;

} else if (g[j][i] == '0') {


++pairs[j].second;

sort(pairs.begin(), pairs.end(),

[](const pair<int, int>& a, const pair<int, int>& b) {

return a.second > b.second;

});

for (int i = 0; i < n; ++i) {

perm[i] = pairs[i].first;

for (int i = 0; i < n; i++) {

cout << perm[i] + 1 << " ";

cout<<endl;

int main() {

ios_base::sync_with_stdio(false);

cin.tie(0);

cout.tie(0);

int t;

cin >> t;

while (t--) {

solve();

return 0;

You might also like