ZOJ - Summer 2018 - Problems - 1001: Saber

本文参考自: 原文地址

Saber is a Class in the Holy Grail War. This Class is regarded as one of the most powerful Class.

“saber”/

Saber is a tree-lover. She loves all kinds of trees. One day, she suddenly comes up with a curious problem. She wants to know that in the path between x and y, whether it exists that when we choose three different edges i,j,k, the length of these three edges can build a triangle. Now she wants you to solve this problem.

Input

There are multiple test cases. The first line of input contains an integer T(T ≤ 5), indicating the number of test cases. For each test case:

The first line contains one integer N(1 ≤ N ≤ 100000), indicating the number of tree’s vertices. In the following N-1 lines, there are three integers x, y, w (1 ≤ w ≤ 1000000000), indicating an edge weighted w between x and y.

In the following line also contains one integer Q(1 ≤ Q ≤ 100000), indicating the number of queries. In the following Q lines, there are two integers x, y, indicating a query between x and y.

Output

For each test case output ‘Case #i:’ in the first line, i equals to the case number. Then for every query output ‘Yes’ or ‘No’ in one line.

Sample Input

2
5
1 2 5
1 3 20
2 4 30
4 5 15
2
3 4
3 5
5
1 4 32
2 3 100
3 5 45
4 5 60
2
1 4
1 3

Sample Output

Case #1:
No
Yes
Case #2:
No
Yes

解题思路:

1、因为题目说明了是树,所以保证给出的数据是一定能通的。2、存在的路径中找到是否存在三条边构成三角形,此时的路径是两点的唯一路径,不包括其他死胡同的边(哪怕是相通的)。3、斐波那契数列与三角形的关联(破 TLE)。


AC 代码(DFS 版)

#include<bits/stdc++.h>
#include<cmath>

#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f

using namespace std;

typedef long long ll;

const int maxn=100000+100;

struct node
{
    int e,w;
    //node(int e,int w):e(e),w(w){} // 推荐这种写法,用到时,无需再定义声明
};

vector<node> ve[maxn];
int vis[maxn], num[60];
int n,m,s,e,flag,found;

void init()
{
    for(int i=0;i<=maxn;i++)
//        ve[i].clear();
        vector<node>().swap(ve[i]); // 这种写法比 clear() 更彻底清除
    m=n-1;
    mem(vis,0);
}

int ok(int a,int b,int c)
{
    return a+b>c;
}

void dfs(int s,int step)
{
    // 50的标记是来自于斐波那契数列上限46就达到了此题目中的最大值
    // 所以顶峰是45时,再无论加一条边就肯定能找到构成的三角形的三条边
    if(step>50 || found) return;

    if(s==e)
    {
        found=1;
        if(step>50)
        {
            flag=1;
            return;
        }

        sort(num,num+step);

        int a,b,c;
        for(int i=2;i<step;i++) // 判断是否可以构成三角形
        {
            a=num[i-2], b=num[i-1], c=num[i];
            if(ok(a,b,c))
            {
                flag=1;
                return;
            }
        }

        return;
    }

    for(int i=0;i<ve[s].size();i++)
    {
        if(!vis[ve[s][i].e])
        {
            vis[ve[s][i].e]=1;
            num[step]=ve[s][i].w;

            dfs(ve[s][i].e,step+1);

            vis[ve[s][i].e]=0;
        }
    }

    return;
}

int main()
{
    int T,kase=0; scanf("%d",&T);
    while(T-- && ~scanf("%d",&n))
    {
        init();
        int u,v,w,q;
        node nd1,nd2;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            nd1.e=v; nd1.w=w;
            nd2.e=u; nd2.w=w;
            ve[u].push_back(nd1);
            ve[v].push_back(nd2);
        }

        scanf("%d",&q);
        printf("Case #%d:\n",++kase);
        for(int i=0;i<q;i++)
        {
            scanf("%d%d",&s,&e);
            found=flag=0;
            vis[s]=1; // 别忘记一开始就已经经过的这个点s
            dfs(s,0);
            vis[s]=0;
            // found == 0 是因为在到终点的途中就已经超过了阈值,所以就可以马上return,此时的found一定等于0
            puts(!found || flag?"Yes":"No");
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39706019/article/details/81560304