http://www.spoj.com/problems/PT07X/ Can someone help me to solve above problem? i m stuck n couldnt find any way through :(
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
http://www.spoj.com/problems/PT07X/ Can someone help me to solve above problem? i m stuck n couldnt find any way through :(
Name |
---|
Well, it's a pretty easy problem. You should maintain a queue of vertices with degree equal to 1 and add their neighbours to the answer, decrease the neighbour's neighbours' degree by 1 and add those which get degree equal to 1 to the queue.
Its a tree .... so it is bipartite. So maximum matching is the answer. As n = 100000, we can use Hopcroft–Karp Bipartite Matching. I got AC using Bipartite Matching.
I think we don't really need a matching. As you mentioned the graph is bipartite. After running a BFS or DFS the answer is the size of the part with minimum elements.
Note: I'm not sure about the correctness of my idea. I'm not home and I can't write it now.
Update: I'm wrong and I'm sorry for confusing you... My code
this code doesn't work for test case 7 (1,2)(1,3)(1,4)(4,5)(4,6)(4,7)
I realized that an year ago :/
Actually, this is a DP problem. Let DP[i][0] be the cost of solving the subtree rooted at i when i itself is not selected, and DP[i][1] be the cost of solving the subtree rooted at i when i is selected. If we don't select i, we must select all its children, otherwise there will be edges with no selected vertex. If we select i, we can freely choose whether or not to select their children when we solve those subtrees. Then...
In the formulas above, c represents the set of children of node i. The answer we seek will be the minimum of DP[1][0] and DP[1][1], assuming we root our tree at node 1.