#include "permgame.h"
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
int n, m;
vector<int> p, chain;
vector<pii> edges;
vector<vector<int> > vect, graph;
bool custom(vector<int> a, vector<int> b){
return a.size()>b.size();
}
int decomp(){
vector<bool> visited(n, 0);
vect.clear();
for (int i=0; i<n; ++i)if (!visited[i]){
vector<int> temp(1, i);
visited[i]=1;
while (!visited[p[temp.back()]])visited[p[temp.back()]]=1, temp.pb(p[temp.back()]);
vect.pb(temp);
}
sort(vect.begin(), vect.end(), custom);
int res=0;
while (vect.size()&&vect.back().size()==1)++res, vect.pop_back();
return n-res;
}
void bob(vector<int> temp){
vector<int> res(m);
for (int i=0; i<m; ++i)res[chain[i]]=temp[i];
int id=Bob(res);
swap(p[res[edges[id].fi]], p[res[edges[id].se]]);
}
int Alice(int M, int e, vector<int> u, vector<int> v, int N, vector<int> P){
n=N, m=M;
p=P;
graph.resize(n);
if (m==2){
decomp();
for (auto c:vect)for (int i=1; i<c.size(); ++i)Bob({c[0], c[i]});
return n;
}
edges.resize(e);
for (int i=0; i<e; ++i){
graph[u[i]].pb(v[i]);
graph[v[i]].pb(u[i]);
edges[i]=mp(u[i], v[i]);
}
int ans=0;
bool die=0;
for (int i=0; i<n; ++i)if (i==p[i])++ans;
for (int i=0; i<n; ++i)if (graph[i].size()>=3)die=1;
if (die||ans>=n-m+1)return ans;
int start=0;
for (int i=0; i<n; ++i)if (graph[i].size()==1)start=i;
chain.resize(1, start);
for (int i=1; i<m; ++i){
if (chain.size()==1||graph[chain.back()][0]!=chain[chain.size()-2])chain.pb(graph[chain.back()][0]);
else chain.pb(graph[chain.back()][1]);
}
if (e==m-1){
while (decomp()>=m){
vector<int> temp;
for (auto c:vect)for (auto a:c)temp.pb(a);
temp.resize(m);
bob(temp);
}
return n-m+1;
}
}
Compilation message (stderr)
permgame.cpp: In function 'int Alice(int, int, std::vector<int>, std::vector<int>, int, std::vector<int>)':
permgame.cpp:78:1: warning: control reaches end of non-void function [-Wreturn-type]
78 | }
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |