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
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » May 2010 Contest » Generalized Spanning Trees

Generalized Spanning Trees

Problem code: GST

  • All Submissions

All submissions for this problem are available.

A spanning tree of an undirected, connected graph (V,E) with vertex set V and edge set E is a subset of edges F such that the graph (V,F) is connected and has no cycles. After a few moments thought, one realizes that an equivalent way to say this is to say:

  • 1) |F| = |V| - 1
  • 2) For any non-empty subset of nodes S, the number of edges with both endpoints in S is does not exceed |S|-1.

There is a fairly simple algorithm that can determine if a set of edges F is indeed a spanning tree of a graph. Simply check that |F| = |V| - 1 and that the graph (V,F) is connected using your favorite connectivity algorithm.

As in all topics in mathematics, the next step is to generalize what is known. So, consider the following generalization of spanning trees. Say a generalized spanning tree (GST) of a graph (V,E) is a function f from the set of edges E = {e1, e2, ..., eM} to the real interval [0,1] such that the following conditions hold:

  • 1) f(e1) + f(e2) + ... + f(eM) = |V| - 1
  • 2) For any non-empty subset of nodes S, the sum of the f(e) values for edges e with both endpoints in S does not exceed |S|-1.

One can see that this generalizes the notion of a spanning tree since spanning trees (in the classical sense) correspond to GSTs by setting f(e) = 1 if e is in the spanning tree and f(e) = 0 if e is not in the spanning tree.

Your task is to determine if a given function is a GST of a given graph.

Input:

The first line contains a single integer T (about 20) indicating the number of test cases to follow.

Each test case begins with two integers N and M where N is the number of nodes and M is the number of edges. The following M lines describe the edges, one per line. Each line contains three values u, v, f where 1 <= u, v <= n are the endpoints of the edge and f is a rational number between 0 and 1 (inclusive). This rational number is the value of the edge in the proposed GST.

The rational numbers are always presented as n/d (even if d=1) where both n and d are non-negative and have no common factors.

The input graphs have no parallel edges or loops and are, of course, undirected. Consecutive cases will be separated by a blank line (including a blank line preceding the first case).

Bounds: 1 <= N <= 50, 1 <= M <= 100, and no denominator of any fraction in the input exceeds 30.

Output:

If the function is a GST, you are to simply print a single line saying "GST".

If the first constraint (the sum of the values of all edges is N-1) is violated, then you should print a single line displaying just the number 1.

Otherwise, if constraint 1) holds but constraint 2) is violated for some non-empty set S, you should print three lines. The first line simply contains the integer 2. The second contains the size of a non-empty subset of the nodes whose corresponding constraint is violated. The third line lists the nodes (in any order) that are part of such a set with a violated constraint (the number of nodes is as reported in the second line).

If there are multiple non-empty sets whose constraints are violated, then any will do. Print a blank line after the output for each case.

Example:

Input:

3

3 3
1 2 2/3
2 3 2/3
3 1 2/3

3 3
1 2 1/1
2 3 1/1
3 1 1/1

4 6
1 2 1/5
1 3 1/5
1 4 1/5
2 3 4/5
2 4 4/5
3 4 4/5

Output:

GST

1

2
3
2 3 4


Author: friggstad
Date Added: 10-04-2010
Time Limit: 7 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

Can anyone explain this

anshulgoyal @ 6 May 2010 06:38 PM

Can anyone explain this testcase :-

4 6
1 2 1/5
1 3 1/5
1 4 1/5
2 3 4/5
2 4 4/5
3 4 4/5

o/p
2
3
2 3 4




What part of that output

triplem @ 7 May 2010 03:03 AM

What part of that output don't you understand?

I have named my class as main

Bibek @ 9 May 2010 12:07 PM

I have named my class as main yet it shows compilation error that i should name my class as main..

What is the error?

Capitalization.

triplem @ 9 May 2010 02:15 PM

Capitalization.

If there are no loops, then

ravi_villuri @ 9 May 2010 04:27 PM

If there are no loops, then for n=1, we should not have edges. But m>=1, so is n=1 a possibility for this problem? Please clarify

You can assume N >= 2.

zac_adm @ 10 May 2010 08:59 AM

You can assume N >= 2.

at the verdict page it

white_king @ 10 May 2010 09:49 AM

at the verdict page it says: :( internal error occurred in the system

 

but it mailed me, AC. So should i assume that, i got an AC?

OK , so I learn your

vdmedragon @ 10 May 2010 09:58 AM

OK , so I learn your technique.

how long they will send you

vdmedragon @ 10 May 2010 10:02 AM

how long they will send you the status of submission?

it takes me about 5 minutes

vdmedragon @ 10 May 2010 10:19 AM

it takes me about 5 minutes to receive a result of submission. "Edit profile" option if you want to do this.

Admins.   Why have I vanished

ravi_villuri @ 10 May 2010 10:05 PM

Admins.

 

Why have I vanished from the all the ratings altogether. I have solved 3 !! :)

@Ravi: The issue is resolved

admin @ 11 May 2010 08:18 PM

@Ravi: The issue is resolved now. Your name appears there :-)

Well, I've spent many hours

triplem @ 12 May 2010 03:06 PM

Well, I've spent many hours on varying approaches to this problem; the closest I've got is that it is possible to find the densest subgraph with min-cut, where density = weight of edges / number of nodes. Here that would only work if we could find the biggest (edges+1)/nodes or edges/(nodes-1), so that doesn't really help at all.

Any hints towards the correct approach?

It's all about finding the

pieguy @ 12 May 2010 03:27 PM

It's all about finding the right google search term.  In this case it's "arboricity" or "Nash-Williams".

@Stephen I solved it with

gmark @ 12 May 2010 03:46 PM

@Stephen

I solved it with maximum density subgraph, or actually using an algorithm that is able to check whether the density is greater than some given parameter. This can be done with one min-cut computation, see for instance http://www.eecs.berkeley.edu/Pubs/TechRpts/1984/CSD-84-171.pdf.

As you say this doesn't quite work since we need to check whether there exists a subgraph such that (weight of edges + 1)/number of nodes in the subgraph > 1, and we can only check that (weight of edges)/number of nodes > 1. However if we knew an edge that is part of a subgraph violating the constraint (weight of edges + 1)/number of nodes, then we could simply add one to its weight and we'd be able to verify the constraint. Since we don't know such an edge, we can just try all edges one-by-one, add one to its weight, and the check the constraint. I'll omit the remaining details of the proof for why this works.

I see. So I was just 'add

triplem @ 12 May 2010 04:00 PM

I see. So I was just 'add one' away from a solution the whole time. Sigh.

Lets assume we already know a

akuegel @ 12 May 2010 04:15 PM

Lets assume we already know a vertex v which belongs to a subset of nodes which does not fulfill constraint 2). We start with a subset consisting of only vertex v. We can then see what happens each time when we connect another node u to the current subset. We add the weight of all edges with endpoint u and the other endpoint in the current set, and subtract 1 because the size of the subset increased by 1. Whenever we get a sum  > 0 after such a step, we have found an inconsistent subset.

Now I used the following idea: each edge needs to give its weight to its adjacent vertices, where each vertex can have a total weight of at most 1 (and vertex v can have a total weight of 0). This can be modeled as a network flow problem: create a node for each edge, and connect the source to each edge node with capacity corresponding to the weight of the edge, then connect each edge node to the nodes which are its endpoint, and each original node is connected to the sink and has capacity 1 (or capacity 0 in the case of vertex v). The network flow algorithm tries to fill each edge from the source to an edge node up to its capacity. The first time when an edge cannot be filled to capacity, we can find the inconsistent subset by calculating which nodes belong to the minimum cut.

Since we originally do not know which vertex v belongs to an inconsistent set, we have to run the algorithm n times (each time selecting another node as vertex v).

@Mark Wow...that's a

pieguy @ 12 May 2010 04:25 PM

@Mark

Wow...that's a completely different (and from the looks of it, much simpler) approach than mine.  My approach was to partition the graph into spanning trees using ideas from https://www.cs.colorado.edu/department/publications/reports/docs/CU-CS-446-89.pdf.  I wouldn't be suprised if there's some test case that my program times out on (since the total number of spanning trees is the LCM of the denominators).

I did an slightly different

balakrishnan_v @ 12 May 2010 05:55 PM

I did an slightly different way. First we need to find the maximum weighted clique where the weight of the clique is the sum of the edges-weights - |V|. If we have the weight of a clique G', we can compute the weight of a clique G'U{v} and G'U{u}U{v} where u and v are vertices not in G'. I start with a G' which is randomly initialized(I try all sizes from 1 to |V|-1). For each size I try several random initializations. Then I do a gradient ascent type approach, where at each step, I pick a vertex or a pair of vertices which maximally increase the weight of the clique. If the weight of the maximal clique is more than -1, then we have a violated constraint 2. Otherwise, we continue doing the iteration described above until we have reached a certain level of confidence.

Although this seems probabilistic, it seemed to work(matches with Brute-force) on several random testcases of sizes<=22. However the exact computation of the probability of false-detection would be of interest for this randomized algorithm.

This problem can be reduced

subra @ 12 May 2010 06:32 PM

This problem can be reduced to http://www.spoj.pl/problems/PROFIT/ with a slight modification. That problem itself is a special case of a more general problem found in CLR exercises on Network Flows (Space shuttle experiments I think), which can be solved by an algorithm similar to the one mentioned by Adrian Keugel.

Briefly, given the costs of choosing vertices and profits arising out of edges connecting two chosen vertices, consider the problem of maximizing the net profit from a subset of vertices  ( = sum of profits of edges with endpoints in the subset minus costs of vertices in the subset). Solving GST then reduces to checking if a profit greater than -1 can be achieved. (Vertices cost 1 each and edge profit = edge weight)

You can solve this problem by the method Adrian Kuegel suggested. Constructing the Network flow graph and finding its max flow. The only problem remaining is we need to avoid the trivial min cut of the flow graph (this corresponds to choosing the empty set of vertices and thus a net profit of 0, but we need to find the best net profit arising out of a non-empty set of vertices).

To do that, you can force the presence of an edge by sufficiently incentivizing its profit by adding an amount, say W, where |V| << W << INF (INF is infinite flow capacity in your flow graph). Then running the maxflow gives you the max-profit with that edge included (after subtracting W). If it is > -1, we have found a violation of property 2. Report this maximizing subset as a witness for violation of 2nd constraint. The subset construction was not straightforward (not every min cut suffices), i used some heuristic, not sure if it is correct.

You can run once for each edge. Including an edge translates to increasing the capacity of one edge in the flow graph. Since the structure of the flow network changes very little in varying the edge to be included, you can "unflow" from the previous max flow computation to remove the excess capacity of the previous edge, increase capacity for the new edge and resume the max-flow, instead of starting from scratch each time.

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 algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm 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 programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were 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 challenges 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 coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

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. You can also host a coding contest for your institute on CodeChef, organize an algorithm event 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