Submission #7123821
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<vi> s; uf(int n){ p.resize(n); h.resize(n); s.resize(n); rep(i, n){ p[i] = i; s[i].pb(i); } } int root(int x){ if(x < 0 || x >= n) exit(0); //????? return x == p[x] ? x : (p[x] = root(p[x])); } bool merge(int a, int b, ll b_minus_a){ if(a < 0 || b < 0 || a >= n || b >= n) exit(0); //???? 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].pb(i); h[i] += d; } s[b].erase(all(s[b])); return 1; } }; bool check(const vector<vector<pi>> &es){ int n = es.size(); uf *u_ = new uf(n); uf &u = *u_; for(const auto &v : es) if(v.size() > 1){ rep(j, v.size() - 1){ pi a = v[j], b = v[j + 1]; if(!u.merge(a.first, b.first, b.second - a.second)) return 0; } } map<int,ll> mnR; for(const auto &v : es) for(const 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(const auto &v : es) for(const 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 | 3245 Byte |
Status | CE |
Compile Error
./Main.cpp: In member function ‘int uf::root(int)’: ./Main.cpp:29:20: error: ‘n’ was not declared in this scope if(x < 0 || x >= n) exit(0); //????? ^ ./Main.cpp: In member function ‘bool uf::merge(int, int, ll)’: ./Main.cpp:33:29: error: ‘n’ was not declared in this scope if(a < 0 || b < 0 || a >= n || b >= n) exit(0); //???? ^