CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • May Cook-Off
    • May Long 2012
    • April Cook-Off
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » April Long Contest » Generalized Independent Sets

Generalized Independent Sets

Problem code: GENIND

  • All Submissions

All submissions for this problem are available.

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.

Input

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.

Output

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.

Example

Input:
3
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

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


Date: 2011-01-05
Time limit: 8s
Source limit: 50000
Languages: TCL PERL6 CLOJ GO PYTH 3.1.2 F# C C99 strict C++ 4.0.0-8 C++ 4.3.2 PAS gpc PAS fpc JAVA NICE JAR C# NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp SCALA HASK ERL CAML CLPS PRLG WSPC BF ICK TEXT JS


  • Submit

Comments

  • Login or Register to post a comment.

The problem statement says:

pragrame @ 6 Apr 2011 01:30 AM

The problem statement says: Finally, for each v, we must have 0 <= f(v) <= 1.

Further, the input specifications don't specify that f(vi) would be in this range.

What should we output if there is NO BAD CYCLE, but f(v) > 1 (in the input) for some ISOLATED VERTEX v?

Did you read the last line of

triplem @ 6 Apr 2011 02:35 AM

Did you read the last line of the input section?

Oops :P

pragrame @ 6 Apr 2011 08:08 PM

Oops :P

I am not able to understand

arunabh2k @ 9 Apr 2011 10:45 PM

I am not able to understand second case, sum of the cycle is 2.00 and floor of 5 node is also 2.00.

How is this bad cycle ?

 

Thanks,

Arunabh

sum of cycle is not 2.001

mkagenius @ 10 Apr 2011 01:56 AM

sum of cycle is not 2.001

i meant, it is 2.001  .

mkagenius @ 10 Apr 2011 01:57 AM

i meant, it is 2.001  .

sorry my bad, thanks

arunabh2k @ 10 Apr 2011 03:07 AM

sorry my bad, thanks

what is this

aaafromlnct @ 15 Apr 2011 06:16 PM

what is this man...............

#include <cstdio>#include

aaafromlnct @ 15 Apr 2011 06:20 PM
  • #include <cstdio>
  • #include <algorithm>
  • using namespace std;
  •  
  • class RadixHeap{
  • struct node_t{
  • int key;
  • int bucket;
  • node_t **left, *right;
  • } *node, *buckets[30];
  • int capacity, B;
  • int upper[30];
  • void prepend(node_t *n){
  • n->left=&buckets[n->bucket];
  • if((n->right=buckets[n->bucket]))
  • n->right->left=&n->right;
  • buckets[n->bucket]=n;
  • }
  • public:
  • void clear(){
  • for(int i=0; i<B-1; i++)
  • upper[i]=(1<<i)-1;
  • upper[B-1]=~(1<<31);
  • for(int i=0; i<B; i++)
  • buckets[i]=NULL;
  • }
  • RadixHeap(int sz, int hi){
  • node=new node_t[sz];
  • capacity=sz;
  • B=0;
  • while(hi>=1<<B)
  • B++;
  • B+=2;
  • clear();
  • }
  • ~RadixHeap(){
  • delete[] node;
  • }
  • void insert(int v, int k){
  • int b=B-1;
  • while(b>0 && upper[b-1]>=k)
  • b--;
  • node[v].key=k;
  • node[v].bucket=b;
  • prepend(node+v);
  • }
  • void decrease_key(int v, int k){
  • node[v].key=k;
  • if(upper[node[v].bucket-1]>=k){
  • *node[v].left=node[v].right;
  • if(node[v].right)
  • node[v].right->left=node[v].left;
  • do{
  • node[v].bucket--;
  • }while(node[v].bucket>0 && upper[node[v].bucket-1]>=k);
  • prepend(node+v);
  • }
  • }
  • int delete_min(){
  • if(!buckets[0]){
  • int b=1;
  • while(b<B && buckets[b]==NULL)
  • b++;
  • if(b==B)
  • return -1;
  • int mk=buckets[b]->key;
  • for(node_t *n=buckets[b]->right; n; n=n->right)
  • mk=min(mk, n->key);
  • upper[0]=mk;
  • for(int i=1; i<b; i++)
  • upper[i]=min(upper[i-1]+(1<<(i-1)), upper[b]);
  • for(node_t *n=buckets[b], *next; n; n=next){
  • next=n->right;
  • do{
  • n->bucket--;
  • }while(n->bucket>0 && upper[n->bucket-1]>=n->key);
  • prepend(n);
  • }
  • buckets[b]=NULL;
  • }
  • int ret=buckets[0]-node;
  • if((buckets[0]=buckets[0]->right))
  • buckets[0]->left=buckets;
  • return ret;
  • }
  • };
  •  
  • void go(){
  • int N, M;
  • int v[500];
  • pair<pair<int, int>, int> edges[8000];
  • int edgestart[1001];
  • scanf("%d %d", &N, &M);
  • for(int i=0; i<N; i++){
  • double d;
  • scanf("%lf", &d);
  • v[i]=1000*d+.01;
  • }
  • int done=0;
  • int MM=0;
  • for(int i=0; i<M; i++){
  • int x, y;
  • scanf("%d %d", &x, &y);
  • if(!done){
  • if(v[x]+v[y]>1000){
  • printf("Bad Cycle: %d %d -1n", x, y);
  • done=true;
  • }
  • if(v[x] && v[y]){
  • edges[MM++]=make_pair(make_pair(x, y+N), 1000-(v[x]+v[y]));
  • edges[MM++]=make_pair(make_pair(x+N, y), 1000-(v[x]+v[y]));
  • edges[MM++]=make_pair(make_pair(y, x+N), 1000-(v[x]+v[y]));
  • edges[MM++]=make_pair(make_pair(y+N, x), 1000-(v[x]+v[y]));
  • }
  • }
  • }
  • if(done)
  • return;
  • sort(edges, edges+MM);
  • edgestart[0]=0;
  • for(int i=1; i<=2*N; i++){
  • edgestart[i]=edgestart[i-1];
  • while(edgestart[i]<MM && edges[edgestart[i]].first.first<i)
  • edgestart[i]++;
  • }
  • RadixHeap rh(2*N, 1000);
  • for(int i=0; i<N; i++){
  • rh.clear();
  • int dist[1000], back[1000];
  • fill(dist, dist+2*N, -1);
  • dist[i]=0;
  • rh.insert(i, 0);
  • int next;
  • while((next=rh.delete_min())!=-1 && next!=i+N){
  • for(int e=edgestart[next]; e<edgestart[next+1]; e++){
  • int t=edges[e].first.second;
  • int s=dist[next]+edges[e].second;
  • if(t!=i && s<1000){
  • if(dist[t]==-1){
  • dist[t]=s;
  • back[t]=next;
  • rh.insert(t, s);
  • }else if(dist[t]>s){
  • dist[t]=s;
  • back[t]=next;
  • rh.decrease_key(t, s);
  • }
  • }
  • }
  • }
  • if(dist[i+N]!=-1){
  • for(char mark[500]={0}; !mark[next%N]; next=back[next])
  • mark[next%N]=1;
  • int start=next;
  • next=(next+N)%(2*N);
  • printf("Bad Cycle: ");
  • for(; next!=start; next=back[next])
  • printf("%d ", next%N);
  • printf("-1n");
  • return;
  • }
  • }
  • puts("Ok");
  • }
  •  
  • int main(){
  • int T;
  • for(scanf("%d", &T); T--; )
  • go();
  • return 0;
  • }
  • SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

    Programming Competition Fetching successful submissions
    Directi Go for Gold
    CodeChef is a global programming communityCodeChef hosts online programming competitions
    CodeChef is a non-commercial competitive programming community
    • About CodeChef
    • About Directi
    • CEO's Corner
    • C-Programming
    • Programming Languages
    • Contact Us
    © 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
    In order to report copyright violations of any kind, send in an email to copyright@codechef.com
    CodeChef a product of Directi
    The time now is:
    CodeChef - A Platform for Aspiring Programmers

    CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

    Practice Section - A Place to hone your 'Computer Programming Skills'

    Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

    Compete - Monthly Programming Contests and Cook-offs

    Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

    Discuss

    Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

    CodeChef Community

    As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

    Go For Gold

    The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

    Domain Name Registration, Web hosting, and Website Design provided by BigRock.com