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

 

题意,给定一颗数,询问两个节点的权值是否存在三个值可以构成三角形;

首先根据斐波那契的特性,第四十多项就大于题目给的数据了,那么取一个50;也就是说查询点之间的点的个数大于50那么肯定就是能构成三角形的了;然后就建一下边,跑一下dfs,对区间内的边长排下序跑一边求三角形公式就完事了

代码如下;

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read();
const int maxn=100005;
int Find,flag;
int n,m,T,u,v;
struct node{
    int point;
    ll w;
};
vector <node > va[maxn];
ll path[55];
int vis[maxn];
void init()
{
    for(int i=0;i<=n;i++) va[i].clear();
    memset(vis,0,sizeof vis);
}
void dfs(int s,int step )
{
    if(step>=50||Find) return ;
    if(s==v)
    {
        Find=1;
        if(step>50)
        {
            flag =1;
            return ;
        }
        sort(path,path+step);
        for(int i=2;i<step;i++)
        {
            if( path[i-2]+path[i-1]>path[i])
            {
                flag=1;
                return ;
            }
        }
        return ;
    }
    for(int i=0;i<va[s].size();i++)
    {
        if(!vis[va[s][i].point])
        {
            vis[va[s][i].point]=1;
            path[step]=va[s][i].w;
            dfs(va[s][i].point,step+1);
            vis[va[s][i].point]=0;
        }
    }
}
int main()
{
    T=read();
    for(int cas=1;cas<=T;cas++)
    {
        n=read();
        init();node p;
        for(int i=0;i<n-1;i++)
        {
            scanf("%d %d %lld",&u,&v,&p.w);
            p.point=v;va[u].push_back(p);
            p.point=u;va[v].push_back(p);
        }
        m=read();
        printf("Case #%d:\n",cas);
        for(int i=0;i<m;i++)
        {
            u=read();v=read();
            Find=flag=0;
            vis[u]=1;
            dfs(u,0);
            vis[u]=0;
            printf(!Find||flag?"Yes\n":"No\n");
        }

    }
    return 0;
}


inline int read()
{
    char ch = getchar();int x = 0, f = 1;
    while(ch < '0' || ch > '9'){ if(ch == '-')f = -1;ch = getchar(); }
    while('0' <= ch && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}

存边别用map,用了就tle,然后ump我又写炸了,不知道ump能不能卡过去,存个结构体的ump写起来比较麻烦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值