Submission #7123556
Source Code Expand
#include<bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)n;i++) #define all(c) (c).begin(),(c).end() #define pb push_back #define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0); using namespace std; namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};} template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";} template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";} void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);} typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9; struct uf{ vi p; vector<ll> h; vector<set<int>> s; uf(int n){ p.resize(n); h.resize(n); s.resize(n); rep(i, n){ p[i] = i; s[i].insert(i); } } int root(int x){ return x == p[x] ? x : (p[x] = root(p[x])); } bool merge(int a, int b, ll b_minus_a){ int A = a, B = b; a = root(a); b = root(b); if(a == b){ return h[B] - h[A] == b_minus_a; } if(s[a].size() < s[b].size()){ swap(a, b); b_minus_a *= -1; } p[b] = a; ll d = h[A] - h[B] + b_minus_a; for(int i : s[b]){ s[a].insert(i); h[i] += d; } return 1; } }; bool check(vector<vector<pi>> &es){ int n = es.size(); uf *u_ = new uf(n); uf &u = *u_; rep(i, n) if(es[i].size() > 1){ rep(j, es[i].size() - 1){ pi a = es[i][j], b = es[i][j + 1]; if(!u.merge(a.first, b.first, b.second - a.second)) return 0; } } map<int,ll> mnR; for(auto v : es) for(pi p : v){ int pos = p.first, val = p.second, r = u.root(pos); ll mn = mnR.count(r) ? mnR[r] : 1e18; mnR[r] = mn = min(mn, val - u.h[pos]); } //dbg(mnR); for(auto v : es) for(pi p : v){ int pos = p.first, val = p.second, r = u.root(pos); //dbg(r, mnR[r], u.h[pos], val); if(mnR[r] + u.h[pos] < 0) return 0; } delete u_; return 1; } int main(){ cin.tie(0); cin.sync_with_stdio(0); int h, w, n; cin >> h >> w >> n; vector<vector<pi>> tate(w), yoko(h); rep(i, n){ int y, x, a; cin >> y >> x >> a; y--; x--; yoko[y].emplace_back(x, a); tate[x].emplace_back(y, a); } cout << (check(tate) && check(yoko) ? "Yes" : "No") << endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | D - Grid and Integers |
User | nadeko |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 3099 Byte |
Status | RE |
Exec Time | 342 ms |
Memory | 32000 KB |
Judge Result
Set Name | Sample | All | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 800 | ||||||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt |
All | 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt, 1_40.txt, 1_41.txt, 1_42.txt, 1_43.txt, 1_44.txt, 1_45.txt, 1_46.txt, 1_47.txt, 1_48.txt, 1_49.txt, 1_50.txt, 1_51.txt, 1_52.txt, 1_53.txt, 1_54.txt, 1_55.txt, 1_56.txt, 1_57.txt, 1_58.txt, 1_59.txt, 1_60.txt, 1_61.txt, 1_62.txt, 1_63.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_00.txt | AC | 1 ms | 256 KB |
0_01.txt | AC | 1 ms | 256 KB |
0_02.txt | AC | 1 ms | 256 KB |
0_03.txt | AC | 1 ms | 256 KB |
0_04.txt | AC | 1 ms | 256 KB |
1_00.txt | AC | 21 ms | 15512 KB |
1_01.txt | AC | 22 ms | 15512 KB |
1_02.txt | AC | 22 ms | 15512 KB |
1_03.txt | AC | 11 ms | 15488 KB |
1_04.txt | AC | 22 ms | 15512 KB |
1_05.txt | AC | 11 ms | 15488 KB |
1_06.txt | AC | 261 ms | 28056 KB |
1_07.txt | AC | 252 ms | 28056 KB |
1_08.txt | AC | 206 ms | 23320 KB |
1_09.txt | AC | 108 ms | 23296 KB |
1_10.txt | AC | 186 ms | 21400 KB |
1_11.txt | AC | 58 ms | 19840 KB |
1_12.txt | AC | 199 ms | 31872 KB |
1_13.txt | AC | 100 ms | 31744 KB |
1_14.txt | AC | 201 ms | 31768 KB |
1_15.txt | AC | 106 ms | 31744 KB |
1_16.txt | AC | 194 ms | 31512 KB |
1_17.txt | AC | 106 ms | 31872 KB |
1_18.txt | AC | 105 ms | 31488 KB |
1_19.txt | AC | 110 ms | 32000 KB |
1_20.txt | AC | 30 ms | 7936 KB |
1_21.txt | WA | 28 ms | 10496 KB |
1_22.txt | WA | 4 ms | 2688 KB |
1_23.txt | WA | 61 ms | 17920 KB |
1_24.txt | AC | 38 ms | 9216 KB |
1_25.txt | AC | 32 ms | 9088 KB |
1_26.txt | WA | 4 ms | 2048 KB |
1_27.txt | WA | 18 ms | 13184 KB |
1_28.txt | AC | 47 ms | 10752 KB |
1_29.txt | RE | 342 ms | 9600 KB |
1_30.txt | AC | 52 ms | 17792 KB |
1_31.txt | WA | 41 ms | 17920 KB |
1_32.txt | AC | 35 ms | 5760 KB |
1_33.txt | AC | 59 ms | 17664 KB |
1_34.txt | WA | 16 ms | 9472 KB |
1_35.txt | WA | 34 ms | 3584 KB |
1_36.txt | WA | 9 ms | 4608 KB |
1_37.txt | AC | 18 ms | 3968 KB |
1_38.txt | WA | 72 ms | 19072 KB |
1_39.txt | RE | 109 ms | 11264 KB |
1_40.txt | RE | 134 ms | 11648 KB |
1_41.txt | RE | 116 ms | 15488 KB |
1_42.txt | AC | 39 ms | 8448 KB |
1_43.txt | RE | 127 ms | 12032 KB |
1_44.txt | RE | 128 ms | 6400 KB |
1_45.txt | AC | 16 ms | 10240 KB |
1_46.txt | AC | 50 ms | 16128 KB |
1_47.txt | WA | 20 ms | 12416 KB |
1_48.txt | AC | 15 ms | 4992 KB |
1_49.txt | WA | 13 ms | 7168 KB |
1_50.txt | AC | 25 ms | 6784 KB |
1_51.txt | RE | 134 ms | 8960 KB |
1_52.txt | WA | 21 ms | 14080 KB |
1_53.txt | WA | 21 ms | 14080 KB |
1_54.txt | WA | 16 ms | 6016 KB |
1_55.txt | RE | 110 ms | 2944 KB |
1_56.txt | AC | 31 ms | 7936 KB |
1_57.txt | AC | 47 ms | 11008 KB |
1_58.txt | RE | 131 ms | 9856 KB |
1_59.txt | WA | 16 ms | 9856 KB |
1_60.txt | AC | 1 ms | 256 KB |
1_61.txt | AC | 1 ms | 256 KB |
1_62.txt | AC | 1 ms | 256 KB |
1_63.txt | AC | 1 ms | 256 KB |