TC SRM 665 DIV2 B LuckyCycle 暴力

LuckyCycle
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87326#problem/B

Description

    
This problem is about trees. A tree consists of some special points (called nodes), and some lines (called edges) that connect those points. Each edge connects exactly two nodes. If there are N nodes in a tree, there are exactly N-1 edges. The edges of a tree must connect the nodes in such a way that the tree is connected: it must be possible to get from any node to any other node by traversing some sequence of edges. Note that this implies that a tree never contains a cycle: for each pair of nodes there is exactly one way to reach one from the other without using the same edge twice.

Dog has a tree. The edges in Dog's tree have weights. As Dog likes the numbers 4 and 7, the weight of each edge is either 4 or 7.

Cat loves modifying trees. Cat is now going to modify Dog's tree by adding one new edge. The new edge will also have a weight that is either 4 or 7. The new edge will connect two nodes that don't already have an edge between them. Note that adding any such edge will create exactly one cycle somewhere in the tree. (A cycle is a sequence of consecutive edges that starts and ends in the same node.)

A cycle is balanced if the number of edges on the cycle is even, and among them the number of edges with weight 4 is the same as the number of edges with weight 7. Cat would like to add the new edge in such a way that the cycle it creates will be balanced.

You are given the description of Dog's current tree in vector <int>s edge1, edge2, and weight. Each of these vector <int>s will have exactly n-1 elements, where n is the number of nodes in Dog's tree. The nodes in Dog's tree are labeled 1 through n. For each valid i, Dog's tree contains an edge that connects the nodes edge1[i] and edge2[i], and the weight of this edge is weight[i].

Return a vector <int> with exactly three elements: {P,Q,W}. Here, P and Q should be the nodes connected by the new edge, and W should be the weight of the new edge. (Note that P and Q must be between 1 and N, inclusive, and W must be either 4 or 7.) If there are multiple solutions, return any of them. If there are no solutions, return an empty vector <int> instead.

 

Input

N will be between 2 and 100, inclusive.
edge1, edge2, and weight will each contain exactly N-1 elements.
Each element of weight will be either 4 or 7
Each element of edge1 and edge2 will be between 1 and N, inclusive.
The input will define a tree.

Output

vector <int> getEdge(vector <int> edge1, vector <int> edge2, vector <int> weight)

Sample Input

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
{4, 4, 4, 4, 4, 4, 7, 7, 7, 7, 7, 7}

Sample Output

Returns: {1, 12, 7 }

HINT

 

题意

给你一颗树,然后让你连两个点,要求连成一个环,然后这个环上面4的边数和7的边数是一样的

题解

数据范围只有100,所以直接n^3暴力就好了

代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;


class LuckyCycle{
public:
    int Ma[150][150];
    int vis[300];
    int ans1=0;
    int ans2=0;
    struct node
    {
        int x,y;
    };
    vector<node> E[300];
    void dfs(int x,int y,int a,int b)
    {
        if(x==y)
        {
            ans1=a;
            ans2=b;
            return;
        }
        if(vis[x])
            return;
        vis[x]=1;
        for(int i=0;i<E[x].size();i++)
        {
            if(E[x][i].y==4)
                dfs(E[x][i].x,y,a+1,b);
            else
                dfs(E[x][i].x,y,a,b+1);
        }
    }
    int check(int i,int j)
    {
        if(Ma[i][j]!=0)
            return 0;
        memset(vis,0,sizeof(vis));
        dfs(i,j,0,0);
        if(ans1==ans2-1)
            return 4;
        if(ans1-1==ans2)
            return 7;
        return 0;
    }
    vector <int> getEdge(vector <int> edge1, vector <int> edge2, vector <int> weight)
    {
        memset(Ma,0,sizeof(Ma));
        for(int i=1;i<=edge1.size()+1;i++)
            Ma[i][i]=1;
        for(int i=0;i<edge1.size();i++)
        {
            Ma[edge1[i]][edge2[i]]=weight[i];
            Ma[edge2[i]][edge1[i]]=weight[i];
            E[edge1[i]].push_back((node){edge2[i],weight[i]});
            E[edge2[i]].push_back((node){edge1[i],weight[i]});
        }
        vector<int> ans;
        int flag = 0;
        for(int i=1;i<=edge1.size()+1;i++)
        {
            for(int j=1;j<=edge1.size()+1;j++)
            {
                ans1=0;
                ans2=0;
                int x = check(i,j);
                if(x>0)
                {
                    ans.push_back(i);
                    ans.push_back(j);
                    ans.push_back(x);
                    flag = 1;
                    break;
                }
            }
            if(flag == 1 )
                break;
        }
        return ans;

    }
};

 

posted @ 2015-08-11 21:10  qscqesze  阅读(258)  评论(0编辑  收藏  举报