Input :
Code: Select all
3
5 0
8 4
1 2
3 4
5 6
6 8
2 1
1 2
Code: Select all
5
1 2 3 4 5
5
2 4 5 7 8
1
2
Moderator: Board moderators
Code: Select all
3
5 0
8 4
1 2
3 4
5 6
6 8
2 1
1 2
Code: Select all
5
1 2 3 4 5
5
2 4 5 7 8
1
2
Code: Select all
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<iostream>
#include<sstream>
using namespace std;
#define INF (1<<30)
vector<vector<int>> adjList;
class StoreAns{
public:
int count ;
string seq ;
};
StoreAns findMax(queue<int> q, char color[],vector<int> d){
int v = q.front();q.pop();
bool any = false;
for(int i = 0 ; i < adjList[v].size();i++){
int k = adjList[v][i];
if(d[k] == INF){
any = true;
q.push(k);
color[k] = 'w';
d[k] = d[v] + 1;
StoreAns a1 = findMax(q,color,d);
if(color[v] != 'b'){
color[k] = 'b';
StoreAns a2 = findMax(q,color,d);
a2.count = a2.count + 1;
if(a1.count > a2.count) return a1;
std::stringstream storestringnum;
storestringnum << (k+1);
a2.seq = storestringnum.str()+ " " + a2.seq;
return a2;
}else return a1;
}//end inf chk
}
if(!any) {
StoreAns temp;
temp.count = 0;
temp.seq = "";
return temp;
}
}
StoreAns printMaxBlack(int nn){
vector<int> d(adjList.size(), INF);
d[0] = 0;
char color[105];
queue<int> q; q.push(0); color[0] = 'w';
StoreAns a1 = findMax(q,color,d);
color[0] = 'b';
StoreAns a2 = findMax(q,color,d); a2.count = a2.count+1;
if(a1.count > a2.count) return a1;
std::stringstream storestringnum;
storestringnum << 1;
a2.seq = storestringnum.str() + " " + a2.seq;
return a2;
}
int main(){
//string r = "";
//int num = 100;
//char x[21];
//std::stringstream storestringnum;
//storestringnum << num;
//
//string y = storestringnum.str() + " " + r;
//cout<<"The concat is: " << y;
freopen("in.txt","r",stdin);
for(int i = 0 ; i < 105;i++) adjList.push_back(vector<int>());
int ng,nn,ne,n1,n2;
scanf("%d",&ng);
for(int x = 0 ; x < ng; x++){
scanf("%d %d",&nn,&ne);
for(int i = 0 ; i < nn;i++) adjList[i].clear();
for(int i = 0; i < ne; i++){
scanf("%d %d",&n1,&n2);
adjList[n1-1].push_back(n2-1);
adjList[n2-1].push_back(n1-1);
}
StoreAns temp = printMaxBlack(nn);
printf("The answer is: %d\n",temp.count);
cout<<temp.seq<<endl;
}//end tc for
return 0;
}
Code: Select all
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
#define PI acos(-1)
#define sqr(x) ((x) * (x))
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define ALL(c) (c).begin(), (c).end()
#define SIZE(c) (int)(c).size()
#define REP(i, a, b) for (int i = int(a); i <= int(b); i++)
#define REPD(i, a, b) for (int i = int(a); i >= int(b); i--)
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<double,double> dd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef map<int,int> mii;
typedef set<int> si;
vi B;
const int MAXN=100;
bool G[MAXN+1][MAXN+1]={{false}};
#define PROD //clocking off
int main() {
#ifndef PROD
clock_t stop_s,start_s;
start_s=clock();
#endif
ll k, n, m;
cin >> k;
while(k--) {
cin >> n >> m;
REP(i, 0, MAXN) { REP(j,0,MAXN) { G[i][j]=false; } }
B.clear();
int u, v;
REP(i, 1, m) { cin >> u >> v; G[u][v]=true; G[v][u]=true; }
ll r= 1 << (n+1);
bool adj=false;
vi s;
REP(i, 0, r) {
if(__builtin_popcount(i) > SIZE(B)) { REP(j, 1, n) { if(i & (1<<j)) { s.PB(j); } } }
else { continue; }
REP(j, 0, SIZE(s)-1) {
REP(t, j+1, SIZE(s)-1) { if(G[s[j]][s[t]]) { adj=true; break; } }
if(adj) { break; }
}
if(!adj && SIZE(s) > SIZE(B)) { B=s; }
s.clear();
adj=false;
}
cout << SIZE(B) << endl;
REP(i, 0, SIZE(B)-1) { if(i > 0) { cout << " " << B[i]; } else { cout << B[i]; } }
if(k > 0) { cout << endl; }
}
#ifndef PROD
stop_s=clock();
cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;
#endif
return 0;
}
Code: Select all
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
#define PI acos(-1)
#define sqr(x) ((x) * (x))
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define ALL(c) (c).begin(), (c).end()
#define SIZE(c) (int)(c).size()
#define REP(i, a, b) for (int i = int(a); i <= int(b); i++)
#define REPD(i, a, b) for (int i = int(a); i >= int(b); i--)
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<double,double> dd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef map<int,int> mii;
typedef set<int> si;
vi B;
const int MAXN=100;
bool G[MAXN+1][MAXN+1]={{false}}, poss[2]={false, true}, cur[MAXN+1]={false};
int n, m, k;
void check() {
vi s;
REP(i, 1, n) { if(cur[i]) { s.PB(i); } }
if(SIZE(s)<SIZE(B)) { return; }
REP(j, 0, SIZE(s)-1) {
REP(t, j+1, SIZE(s)-1) { if(G[s[j]][s[t]]) { return; } }
}
if(SIZE(s) > SIZE(B)) { B=s; }
}
void solve(int l, int c) {
if(l>n) { check(); return; }
else if((n-l)+c <= SIZE(B)) { return; }
REP(i, 0, 1) {
cur[l]=poss[i];
solve(l+1, poss[i]?c+1:c);
}
}
//#define PROD //clocking off
int main() {
#ifndef PROD
clock_t stop_s,start_s;
start_s=clock();
#endif
cin >> k;
while(k--) {
cin >> n >> m;
REP(i, 0, MAXN) { memset(G[i], false, (MAXN+1) * sizeof(bool)); }
memset(cur, false, (MAXN+1) * sizeof(bool));
B.clear();
int u, v;
REP(i, 1, m) {
cin >> u >> v;
G[u][v]=true; G[v][u]=true;
}
solve(1, 0);
cout << SIZE(B) << endl;
REP(i, 0, SIZE(B)-1) { if(i > 0) { cout << " " << B[i]; } else { cout << B[i]; } }
if(k > 0) { cout << endl; }
}
#ifndef PROD
stop_s=clock();
cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;
#endif
return 0;
}