Page 6 of 7

### Re: 193 Graph Coloring

Posted: Fri Aug 17, 2012 10:16 pm
Try this Input who are getting WA Input :

Code: Select all

``````3

5 0

8 4
1 2
3 4
5 6
6 8

2 1
1 2``````
Output :

Code: Select all

``````5
1 2 3 4 5
5
2 4 5 7 8
1
2
``````

### Re: 193 Graph Coloring

Posted: Sat Feb 23, 2013 3:49 pm
Hi Everyone,

Below is my code for this problem. It uses simple Breadth first search. Moreover, for each node, it tries to color it both white and black(assuming adjacent node is not black). I have no idea why my output in not right. I will be grateful if someone could take a look and tell what is wrong with my code. If you tell me mistake, it will motivate me to solve more problems and becoem better. PLEASE HELP

Code: Select all

``````#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<iostream>
#include<sstream>
using namespace std;
#define INF (1<<30)
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++){
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){
d = 0;
char color;
queue<int> q; q.push(0); color = 'w';
StoreAns a1 = findMax(q,color,d);
color = '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;
//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);
}
StoreAns temp = printMaxBlack(nn);
cout<<temp.seq<<endl;

}//end tc for

return 0;

}``````

### Re: 193 Graph Coloring

Posted: Sat Feb 23, 2013 9:30 pm
@problemsolve I don't know why but I used dfs got WA then tried using backtracking and even though I thought I might get TLE got AC!:)

Can anyone explain why? Specially if you've solved it using BFS or DFS and got AC? Thanks.

### Re: 193 Graph Coloring

Posted: Fri Aug 09, 2013 12:25 am
I first tried to solve this problem based on DFS, trying all initial possibilities as black, visiting all nodes and verifying if any neighbour was black before painting. But this approach was not covering all possibilities, for example, we can have a scenario where two white nodes in a row could lead us to more black nodes than one white and the other black.
For this reason, I tried another approach and got AC with backtracking, using basically the same logic we have to generate all permutations, so unlike DFS I'm not visiting neighbour nodes, nodes are visited according to their index, generating all possibilities. I also added some pruning constraints, like stopping if the number of remaining nodes to visit cannot reach the maximum number of black nodes or if the number of black nodes is equal the number of nodes.
I hope this can help.

### Re: 193 Graph Coloring

Posted: Sat Mar 29, 2014 7:26 am
AC ### Re: 193 Graph Coloring

Posted: Tue Apr 01, 2014 9:33 pm

### Re: 193 Graph Coloring

Posted: Thu Apr 03, 2014 12:09 am
AC ### Re: 193 Graph Coloring

Posted: Fri Apr 04, 2014 12:08 am
That is AC code

### Re: 193 Graph Coloring

Posted: Mon Jun 02, 2014 6:19 am
i used bcktracking algorithm but i m getting PE..presentation error
http://ideone.com/Yf5DLo
need help

### Re: 193 Graph Coloring

Posted: Mon Jun 02, 2014 6:20 am
AC ### Re: 193 - Graph Coloring

Posted: Thu Feb 19, 2015 7:33 pm
Anybody can help me with counter example, or insight as to why WA? I dont want the answer, just direction. Thanks guys.

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);
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 && SIZE(s) > SIZE(B)) { B=s; }
s.clear();
}

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;
}``````

### Re: 193 - Graph Coloring

Posted: Thu Feb 19, 2015 10:15 pm
You can solve this using backtracking.

### Re: 193 - Graph Coloring

Posted: Thu Feb 19, 2015 10:22 pm
backtrack = complete search, thats what I am doing already. I have correct answer for every input i have found so far.

UPDATE- I think i see the problem, the method of generating all subsets doesnt work for n > 64

### Re: 193 - Graph Coloring

Posted: Fri Feb 20, 2015 2:10 am
This is my new complete search method but now I am getting TLE even though I thought my pruning would be sufficient

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={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;
}``````

### Re: 193 - Graph Coloring

Posted: Fri Feb 20, 2015 4:03 am
pruning tips anybody, please?? 