Given a graph G on nodes V with undirected edges E, an independent set X is a subset of V such that no edge in E has both endpoints in X. Finding the size of the largest independent set in a graph is currently a very difficult problem.

Consider the following generalization of an independent set. A function f that maps each node v to a non-negative value f(v) is said to be a partially generalized independent set if, for any edge e = (u,v), we have f(u) + f(v) ≤ 1. Unfortunately, this doesn’t provide a great generalization. Consider a graph G where every possible edge exists in E. The size of a maximum independent set is only 1 whereas assigning 1/2 to every value f(v) has the total fractional value of the f(v) values being |V|/2.

So, let’s add one more constraint to strengthen the notion of a generalized independent set. A cycle in a graph is a sequence of k ≥ 2 distinct nodes v1, v2, …, vk where there is an edge between vi and vi+1 for any 1 ≤ i < k as well as an edge between v1 and vk. Notice that cycles of length 2 (i.e. edges) are permitted by this definition.

If C is a cycle of G of length |C|, then no independent set can have more than floor(|C|/2) nodes from C. Given this, define a generalized independent set as a function f on nodes in V that satisfies the following conditions. For any cycle C = v1, v2, …, v|C|, we have f(v1) + f(v2) + … + f(v|C|) ≤ floor(|C|/2). Finally, for each node v we must have 0 ≤ f(v) ≤ 1.

Your task will be to determine if a given function f on the nodes of a graph is indeed a generalized independent set. If not, you must produce a cycle that is violated.


The first line of the input contains an integer T ≤ 30 indicating the number of test cases. Each test case begins with two integers n and m. Here, n is the number of nodes and m is the number of edges in the graph. Say the nodes of the graph are denoted by v0, v1, …, vn-1. Following this is a line containing n floating point values. The i’th such value is the value of f(vi). Finally, m lines follow, each containing two distinct integers i,j between 0 and n-1 that indicate that there is an edge between vi and vj in the graph. No edge will be given more than once in the input.

Bounds: 1 ≤ n ≤ 500 and 0 ≤ m ≤ 2,000. The value of each f(vi) will be between 0 and 1 (inclusive) and will be presented with at most three decimals of precision.


The output for each test case consists of a single line. If the input function is a generalized independent set then the line consists simply of the text “Ok”. Otherwise, you should output the text “Bad Cycle: ” followed by a sequence of distinct integers between 0 and n-1 that forms a cycle C for which the sum of the f(v) values for nodes in C exceeds floor(|C|/2). Following this sequence is the single integer -1. These integers should be separated by a single space.

If there are multiple such cycles, then any will do. Also, the order in which the nodes of such a cycle are output does not matter beyond ensuring that consecutive nodes in the output are connected by an edge and that the first and last nodes are also connected by an edge.


4 4
0.5 0.5 0.5 0.5
0 1
1 2
2 3
3 0
6 7
0.5 0.5 0.334 0.5 0.333 0.334
0 1
0 2
1 3
2 3
1 4
2 5
4 5
5 5
1.0 0.0 0.667 0.5 0.0
0 1
1 2
2 3
3 4
4 1

Bad Cycle: 4 5 2 0 1 -1
Bad Cycle: 3 2 -1


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <string.h>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)
const double pi=acos(-1.0);
const double eps=1e-9;
template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}
template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}
template<class T> inline T sqr(T x){return x*x;}
typedef pair<int,int> ipair;
#define SIZE(A) ((int)A.size())
#define LENGTH(A) ((int)A.length())
#define MP(A,B) make_pair(A,B)
#define PB(X) push_back(X)

const int maxn=1000+5;
const int maxm=20000+5;

int n,m;
int f[maxn];
int deg[maxn],g[maxn][maxn];
int d[maxn],p[maxn];
int q1[maxn],q2[maxn],idx[maxn],counter;

int main()
int testcase;
for (scanf(“%d”,&testcase);testcase>0;testcase–)
for (int i=0;i<n;i++) { double d; scanf(“%lf”,&d); f[i]=500-(int)(d*1e3+0.5); }
bool exists=false;
for (int i=0;i<n+n;i++) deg[i]=0;
for (int i=0;i<m;i++)
int a,b;
if (exists) continue;
if (f[a]+f[b]<0)
printf(“Bad Cycle: %d %d -1n”,a,b);
if (exists) continue;
for (int i=0;i<n;i++) f[i+n]=f[i];
for (int key=0;key<n;key++)
for (int i=0;i<n+n;i++) d[i]=1000000000;
int sizeq1=0;
while (sizeq1>0)
int sizeq2=0;
for (int cl=0;cl<sizeq1;cl++)
int i=q1[cl];
int exp=d[i]+f[i];
for (int j=0;j<deg[i];j++)
int other=g[i][j];
if (exp<d[other]) { d[other]=exp; p[other]=i; if (idx[other]!=counter) idx[other]=counter,q2[sizeq2++]=other; }
for (int i=0;i<sizeq1;i++) q1[i]=q2[i];
if (d[key+n]<500)
printf(“Bad Cycle:”);
vector<int> path;
for (int k=key+n;k!=key;k=p[k]) path.push_back(k%n);
for (int i=0;i<n;i++) p[i]=-1;
int src=-1,length=1000000000;
for (int i=0;i<SIZE(path);i++)
int s=path[i];
if (p[s]>=0 && i-p[s]<length) length=i-p[s],src=p[s];
for (int k=src;k<src+length;k++) printf(” %d”,path[k]);
printf(” -1n”);
if (!exists) printf(“Okn”);
return 0;