結果

問題 No.1290 Addition and Subtraction Operation
ユーザー CinnamorollCinnamoroll
提出日時 2020-07-07 08:41:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 8,491 bytes
コンパイル時間 4,415 ms
コンパイル使用メモリ 198,284 KB
実行使用メモリ 44,748 KB
最終ジャッジ日時 2023-09-30 02:17:56
合計ジャッジ時間 18,075 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 4 ms
6,208 KB
testcase_03 WA -
testcase_04 AC 5 ms
6,280 KB
testcase_05 RE -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 4 ms
6,160 KB
testcase_09 WA -
testcase_10 AC 4 ms
6,020 KB
testcase_11 WA -
testcase_12 AC 4 ms
6,132 KB
testcase_13 AC 4 ms
6,008 KB
testcase_14 AC 4 ms
6,124 KB
testcase_15 WA -
testcase_16 RE -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 4 ms
6,000 KB
testcase_20 AC 4 ms
6,244 KB
testcase_21 AC 4 ms
6,012 KB
testcase_22 AC 4 ms
6,016 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 RE -
testcase_26 AC 4 ms
6,220 KB
testcase_27 AC 4 ms
5,992 KB
testcase_28 RE -
testcase_29 RE -
testcase_30 WA -
testcase_31 WA -
testcase_32 RE -
testcase_33 AC 4 ms
6,256 KB
testcase_34 WA -
testcase_35 AC 4 ms
6,008 KB
testcase_36 RE -
testcase_37 WA -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 AC 22 ms
8,264 KB
testcase_43 AC 22 ms
8,388 KB
testcase_44 AC 23 ms
8,420 KB
testcase_45 AC 22 ms
8,468 KB
testcase_46 AC 22 ms
8,288 KB
testcase_47 AC 23 ms
8,424 KB
testcase_48 AC 22 ms
8,312 KB
testcase_49 AC 22 ms
8,312 KB
testcase_50 AC 22 ms
8,316 KB
testcase_51 AC 22 ms
8,380 KB
testcase_52 AC 22 ms
8,272 KB
testcase_53 AC 23 ms
8,512 KB
testcase_54 AC 22 ms
8,432 KB
testcase_55 AC 23 ms
8,480 KB
testcase_56 AC 22 ms
8,312 KB
testcase_57 RE -
testcase_58 RE -
testcase_59 RE -
testcase_60 AC 141 ms
37,092 KB
testcase_61 AC 116 ms
41,892 KB
testcase_62 AC 128 ms
41,220 KB
testcase_63 RE -
testcase_64 RE -
testcase_65 RE -
testcase_66 AC 114 ms
44,084 KB
testcase_67 AC 142 ms
38,180 KB
testcase_68 AC 125 ms
41,660 KB
testcase_69 RE -
testcase_70 RE -
testcase_71 RE -
testcase_72 RE -
testcase_73 AC 114 ms
43,336 KB
testcase_74 RE -
testcase_75 RE -
testcase_76 RE -
testcase_77 WA -
testcase_78 WA -
testcase_79 RE -
testcase_80 WA -
testcase_81 WA -
testcase_82 AC 124 ms
44,748 KB
testcase_83 AC 134 ms
37,512 KB
testcase_84 AC 131 ms
38,600 KB
testcase_85 RE -
testcase_86 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

// warm heart, wagging tail,and a smile just for you!
//                                                                     ███████████
//                                                                   ███╬╬╬╬╬╬╬╬╬╬███
//                                                                ███╬╬╬╬╬████╬╬╬╬╬╬███
//                                            ███████████       ██╬╬╬╬╬████╬╬████╬╬╬╬╬██
//                                      █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██
//                               ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██
//                             ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██
//                           ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████
//                         ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██
//                       ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███
//                     ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██
//                 ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████
//     █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████
//   ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████
//   ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██
//       ██████████████  ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████
//                         ███████                           █████  ███████████████████  
//
#include "bits/stdc++.h"
using namespace std;
#define INF (1<<30)
#define LINF (1LL<<60)
#define fs first
#define sc second
#define int long long
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,a,b) for(int i = (b-1);i>=(a);--i)
#define REP(i,n)  FOR(i,0,(n))
#define RREP(i,n) RFOR(i,0,(n))
#define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr,mp) for(auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x)  cout << #x << " = " << (x) << endl
#define SP << " " << 
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
#define MSB(x) (63-__builtin_clzll(x))
#define pcnt(x) (__builtin_popcountll(x))
#define parity(i,j) (i&(1LL<<j))
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

struct UnionFind {
  private:
  vector<int> child,tree;
  vector<vector<int>> list;

public:
  UnionFind(int v) {
    tree.resize(v);
    list.resize(v);
    REP(i,v) tree[i] = i, list[i].push_back(i);
  }

  int root(int i){
    if(i == tree[i]){
      REP(j,child.size()) tree[child[j]] = i;
      child.clear(); 
      return i;
    }
    else{
      child.push_back(i);
      return i = root(tree[i]);
    }
  }

  int size(int i){
    return list[root(i)].size();
  }

  vector<int> nodes(int no){
    return list[root(no)];
  }

  bool unit(int x, int y){
    x = root(x);
    y = root(y);
    if(x==y) return false;
    if(list[x].size() < list[y].size()) swap(x,y);
    for(int no:list[y]) list[x].emplace_back(no);
    tree[y] = x;
    return true;
  }

  bool isUnit(int x, int y){
    return root(x) == root(y);
  }
};

void ng(){
  cout << "NO" << endl;
  exit(0);
}

const int N = 1e5+10;
vector<int> edge[N],color(N,0);

bool isBip(int no){
  if(!color[no]) color[no] = 1;
  bool flag = true;
  for(auto to:edge[no]){
    if(color[no]==color[to]) flag = false;
    else if(!color[to]){
      color[to] = -color[no];
      if(!isBip(to)) flag = false;
    }
  }
  return flag;
}

signed main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n,m;
  cin >> n >> m;

  vector<int> a(n);
  REP(i,n) cin >> a[i];
  
  vector<int> b(n);
  REP(i,n) cin >> b[i];
  
  vector<int> c(n+2,0);
  REP(i,n) c[i+1] = b[i]-a[i];

  vector<int> d(n+1);
  REP(i,n+1) d[i] = c[i]+c[i+1];

  UnionFind uf(n+1);
  vector<P> op;
  REP(i,m){
    int x,y;
    cin >> x >> y;
    if((y-x)%2) uf.unit(x-1,y);
    else op.emplace_back(x-1,y);
  }

  // 連結成分で座圧
  map<int,int> mp;
  REP(i,n+1) mp[uf.root(i)]++;
  int k = 0; // 連結成分数
  ITR(itr,mp) itr->sc = k++;

  vector<int> id(n+1),sum(k,0); // 座圧後のid, 連結成分内のdのsum
  REP(i,n+1){
    id[i] = mp[uf.root(i)];
    sum[id[i]] += d[i];
  }

  vector<int> flag(k,0); // 自己ループを含むか
  UnionFind uf2(k);
  REP(i,op.size()){
    int x = id[op[i].fs], y = id[op[i].sc];
    if(x==y) flag[x]++;
    else{
      uf2.unit(x,y);
      edge[x].emplace_back(y);
      edge[y].emplace_back(x);
    }
  }

  map<int,int> sum2;
  vector<int> flag2(k,0), list[k];
  REP(i,k){
    sum2[uf2.root(i)] += sum[i];
    flag2[uf2.root(i)] += flag[i];
    list[uf2.root(i)].emplace_back(i);
  }

  REP(i,k){
    if(uf2.root(i)!=i) continue;
    if(sum2[i]%2) ng(); // 総和を奇数にはできない
    if(flag2[i]) continue; // 自己ループ
    if(!isBip(i)) continue; // 奇閉路ある?
    int v = 0;
    for(int no:list[i]) v += sum[no]*color[no];
    if(v) ng(); // 和が一致する?
  }

  cout << "YES" << endl;

  return 0;
}
0