Skip to content
Permalink
Browse files

AtCoder CodeFestival2016 quala D

  • Loading branch information...
cielavenir committed Oct 12, 2016
1 parent 6a7d4b4 commit 073d4d0c991750b056836de69cc7b556864b6353
Showing with 85 additions and 0 deletions.
  1. +45 −0 atcoder/tyama_atcodercodefestival2016qualaD.cpp
  2. +40 −0 tyama_utpc2010_10125(aizu2207)_stack.cpp
@@ -0,0 +1,45 @@
#include <cstdio>
#include <unordered_map>
#include <deque>
#include <algorithm>
using namespace std;
typedef int key;

int main(){
int r,c,n;
scanf("%d%d%d",&r,&c,&n);
unordered_map<key,deque<pair<key,long long>>>m;
for(;n;n--){
int s1,s2,d;
scanf("%d%d%d",&s1,&s2,&d);
m[s1].emplace_back(s2+r,d);
m[s2+r].emplace_back(s1,-d);
}
for(;!m.empty();){
long long a=-1LL<<61,b=1LL<<61;
auto s=m.begin()->first;
deque<pair<key,long long>>st={{s,0}};
unordered_map<key,long long>memo={{s,0}};
for(;!st.empty();){
auto p=*st.rbegin();st.pop_back();
auto cur=p.first;long long d=p.second;
for(auto &e:m[cur]){
if(memo.find(e.first)==memo.end()){
st.emplace_back(e.first,d+e.second);
memo[e.first]=d+e.second;
}else if(memo[e.first]!=d+e.second){
puts("No");
return 0;
}
}
if(cur<=r)a=max(a,d);
else b=min(b,d);
}
if(a>b){
puts("No");
return 0;
}
for(auto &e:memo)m.erase(m.find(e.first));
}
puts("Yes");
}
@@ -0,0 +1,40 @@
#include <string>
#include <cstdio>
#include <unordered_map>
#include <deque>
#include <algorithm>
using namespace std;
typedef string key;

char b1[99],b2[99];
void main2(int n){
unordered_map<key,deque<pair<key,long long>>>m;
for(;n;n--){
int d;
scanf(" 1 %s = 10^%d %s",b1,&d,b2);
string s1=b1,s2=b2;
m[s1].emplace_back(s2,d);
m[s2].emplace_back(s1,-d);
}
for(;!m.empty();){
auto s=m.begin()->first;
deque<pair<key,long long>>st={{s,0}};
unordered_map<key,long long>memo={{s,0}};
for(;!st.empty();){
auto p=*st.rbegin();st.pop_back();
auto cur=p.first;long long d=p.second;
for(auto &e:m[cur]){
if(memo.find(e.first)==memo.end()){
st.emplace_back(e.first,d+e.second);
memo[e.first]=d+e.second;
}else if(memo[e.first]!=d+e.second){
puts("No");
return;
}
}
}
for(auto &e:memo)m.erase(m.find(e.first));
}
puts("Yes");
}
int main(){int n;for(;~scanf("%d",&n)&&n;)main2(n);}

0 comments on commit 073d4d0

Please sign in to comment.
You can’t perform that action at this time.