小诚因为金铲铲D不到牌破产啦

小诚因为金铲铲D不到牌破产啦

Description

小诚和他身边的朋友最近好像出了点经济问题……

已知小诚的人际关际网中包含 n* 个人(小诚也在其中),每个人手上现在有ai元,他们可以彼此之间互相借钱,他们只希望在最后手上恰好有 bi

众所周知,欠钱容易借钱难,没借到之前是孙子,借到了之后对面是孙子。所以两个人之间,如果不是关系密切的朋友,就无法放心地互相借钱,小诚通过摸索,知道了他们之间有 m 对关系密切的朋友。

请问所有的人能够通过互相借钱满足他们的需求吗?

注意,有的人可能一开始就是负债的状态,即aibi都有可能是负数

Input

第一行两个整数n,m分别表示人数和关系密切的朋友对数

接下来一行n个整数a1,a2,...,an,表示每个人手上初始有的钱

接下来一行 n个整数 b1,b2,...,bn,表示每个人手上最后希望恰好有的钱

接下来 m 行,每行两个整数 x,y,表示 xy 是关系密切的朋友

1≤n,m≤2e5 ,−1e9≤ai,bi≤1e9

Output

对于每一组数据,输出一行一个字符串,如果可以满足,输出"Yes",否则输出 "No"

Sample Input 1

3 2
1 2 3
2 2 2
1 2
2 3

Sample Output 1

Yes
#include <bits/stdc++.h>
using namespace std;
long long x,y,n,m;
vector<long long> a , b ,suma ,sumb;
vector<int> f;
int find(int x) {
    if (f[x]!= x) {
        f[x] = find(f[x]);
    }
    return f[x];
}
void baka(int x, int y) {
    int findX = find(x);
    int findY = find(y);
    if (findX!= findY) {
        f[findX] = findY;
        suma[findY] += suma[findX];
        sumb[findY] += sumb[findX];
    }
}

int main() {
    scanf("%lld %lld", &n, &m);
    a.resize(n);b.resize(n);f.resize(n);suma.resize(n);sumb.resize(n);
    for (int i = 0; i < n; i++) {
        f[i] = i;
        scanf("%lld", &a[i]);
        suma[i] = a[i];
    }

    for (int i = 0; i < n; i++) {
        scanf("%lld", &b[i]);
        sumb[i] = b[i];
    }

    for (int i = 0; i < m; i++) {
    scanf("%lld %lld", &x, &y);
        baka(x, y);
    }

    for (int i = 0; i < n; i++) {
        int fumo = find(i);
        if (suma[fumo]!= sumb[fumo]) {
            printf("No\n");
            return 0;
        }
    }

    printf("Yes\n");
    return 0;
}



posted @   土木牢盖  阅读(10)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示