2017年上海金马五校程序设计竞赛(网上资格赛)Problem E : A No-story Kingdom

Description
In an unknown island, there is a tree kingdom whose structure is just like a tree. It has a city called root city and as your expectation, this root city has several son cities. And every son city may have its own son cities too. Between every pair of son and father, there is an available road.
The tree kingdom has a legend that once ago a nameless man threw some magic pieces on the ground of the kingdom, there are three types of the magic pieces and if someone who collected all the three types of pieces, he can make a wish which definitely will come true.
And now our leading roles get on the stage. Their names are C and J. They decided to collect all types of pieces because they had a common secret wish. Suppose that all the pieces are left on the roads, C and J start from different cities, and they both go to the fathers of theirs current cities. When they first meet, they will count their pieces. If they get all the three types of pieces, they will make that wish.

Input
There are at most 40 test cases. For each test case, you will get a number NN (2≤N≤10000)(2≤N≤10000) denoting the number of cities.
In the folowing n−1n−1 lines each contains 55 numbers (u,v,a,b,c)(u,v,a,b,c). Among them, uu and vv means that there is an edge between city uu and vv. aa, bb, cc (a,b,c∈{0,1})(a,b,c∈{0,1}) means that the if the road between city uu and vv has the pieces. If the road has the piece of first type, then aa will be 11 or aa will be 00. So do the bb and cc.Notice that all the cities are numbered from 11 to nn, and the root city is numbered by 11. And the road is bi-directional.
Then there is a number mm (1≤m≤n)(1≤m≤n) denoting the number of question.
Each of the next mm lines contains two number pp and qq, means C starts from city pp and J starts from city qq.

Output
For each test case, if C and J can make their dream print “Y” or print “N” instead.

Sample Input
5
1 2 0 0 1
1 3 1 1 0
3 4 0 0 1
3 5 1 0 1
3
1 2
1 5
4 5
5
1 2 0 0 1
1 3 1 1 0
3 4 0 0 1
3 5 1 0 1
3
1 2
1 5
4 5

Sample Output
N
Y
N
N
Y
N

题意:给你一棵树,每个树边上有abc三个东西,用01来表示有没有这个,然后有两个人,分别在树的两个节点上,然后一起往根走,那么他们肯定会在一个节点碰到,问他们碰到的时候是不是能够把abc三个物品都收集到。
思路:很明显用lca。。。嗯。。对头就是lca。。怎么想到的呢。你想嘛。。你从两个节点往上走,然后走到碰到,不就是求他们的公共祖先么。然后我一开始是想把abc三个物品的状态状态压缩一下然后去搞一搞。但是发现不能消除根到祖先的影响。后来想明白了。。记录个数就很好处理了。cur_cnt = cnt[u]+cnt[v] - 2*cnt[anc] 这个公式应该都记得。。就是两个儿子节点的状态加起来然后去掉根节点到祖先的那一段影响。然后就好做了2333

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<map>
#include<set>
using namespace std;

//thanks to pyf ...
//thanks to qhl ...

#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
typedef pair<int, int> PII;
typedef long long ll;

const int N = 1e6 + 5;

struct Edge
{
    int u, v;
    int next;
    int a, b, c;
} edge[N * 2];
struct Query
{
    int u, v, id;
    int next;
} Q[N * 2];
vector<PII>final_q;
int vis[N];
int fa[N];
int ans[N];
int cnt_a[N], cnt_b[N], cnt_c[N];
int head[N];
int qhead[N];
int tot = 0;
int q_tot = 0;
int n;
void init()
{
    final_q.clear();
    tot = 0 ;
    CLR(qhead, -1);
    CLR(vis, 0);
    CLR(fa, 0);
    CLR(ans, 0);
    CLR(cnt_a, 0);
    CLR(cnt_b, 0);
    CLR(cnt_c, 0);
    CLR(head, -1);
}
void add_edge(int u, int v, int a, int b, int c)
{
    edge[tot].u = u;
    edge[tot].v = v;
    edge[tot].a = a;
    edge[tot].b = b;
    edge[tot].c = c;
    edge[tot].next = head[u];
    head[u] = tot ++ ;
}
void add_Q(int u, int v, int id)
{
    Q[q_tot].u = u;
    Q[q_tot].v = v;
    Q[q_tot].id = id;
    Q[q_tot].next = qhead[u];
    qhead[u] = q_tot ++ ;
}
int find(int x)
{
    if (fa[x] != x)
        fa[x] = find(fa[x]);
    return fa[x];
}
void get_inf(int u, int FA)
{
    fa[u] = u;
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].v;
        int a = edge[i].a;
        int b = edge[i].b;
        int c = edge[i].c;
        if (v == FA)
            continue;
        cnt_a[v] += (cnt_a[u] + a);
        cnt_b[v] += (cnt_b[u] + b);
        cnt_c[v] += (cnt_c[u] + c);
        get_inf(v, u);
    }
}
void lca(int u, int FA)
{
    vis[u] = 1;
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].v;
        if (v == FA)
            continue;
        lca(v, u);
        fa[v] = u;
    }
    for (int i = qhead[u]; i != -1; i = Q[i].next)
    {
        int v = Q[i].v;
        if (vis[v])
        {
            ans[Q[i].id] = find(v);
        }
    }
}
bool judge(int u, int v, int id)
{
    if (cnt_a[u] + cnt_a[v] - 2 * cnt_a[id] < 1 || cnt_b[u] + cnt_b[v] - 2 * cnt_b[id] < 1 || cnt_c[u] + cnt_c[v] - 2 * cnt_c[id] < 1)
        return false;
    return true;
}
int main()
{
    while (scanf("%d", &n) == 1)
    {
        init();
        for (int i = 1; i < n; i++)
        {
            int u, v, a, b, c;
            scanf("%d%d%d%d%d", &u, &v, &a, &b, &c);
            add_edge(u, v, a, b, c);
            add_edge(v, u, a, b, c);
        }
        int q;
        get_inf(1, 1);
        scanf("%d", &q);
        for (int i = 0; i < q; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            add_Q(u, v, i);
            add_Q(v, u, i);
            final_q.push_back(mp(u, v));
        }
        lca(1, 1);
        for (int i = 0; i < q; i++)
        {
            // int u = final_q[i].first , v = final_q[i].second;
            // cout << u << " " << v << " " << ans[i] << endl;
            // cout << cnt_a[u] << " " << cnt_b[u] << " " << cnt_c[u] << endl;
            // cout << cnt_a[v] << " " << cnt_b[v] << " " << cnt_c[v] << endl;
            // cout << cnt_a[i] << " " << cnt_b[i] << " " << cnt_c[i] << endl;
            if (judge(final_q[i].first, final_q[i].second, ans[i]))
                printf("Y\n");
            else
                printf("N\n");
        }
    }
}
/*
5
1 2 1 1 1
2 3 0 0 0
3 4 0 0 0
4 5 0 0 0
1
1 4
5
1 2 1 1 1
2 3 0 0 0
3 4 0 0 0
4 5 0 0 0
5
1 2
1 3
2 3
3 4
1 4
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值