|
| 1 | +// BEGIN CUT HERE |
| 2 | + |
| 3 | +// END CUT HERE |
| 4 | +#include <algorithm> |
| 5 | +#include <iostream> |
| 6 | +#include <sstream> |
| 7 | +#include <string> |
| 8 | +#include <vector> |
| 9 | +#include <queue> |
| 10 | +#include <set> |
| 11 | +#include <map> |
| 12 | +#include <cstdio> |
| 13 | +#include <cstdlib> |
| 14 | +#include <cctype> |
| 15 | +#include <cmath> |
| 16 | +#include <string> |
| 17 | +#include <cstring> |
| 18 | +using namespace std; |
| 19 | + |
| 20 | +// BEGIN CUT HERE |
| 21 | +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) |
| 22 | + |
| 23 | +template<typename T> void print( T a ) { |
| 24 | + cerr << a; |
| 25 | +} |
| 26 | +static void print( long long a ) { |
| 27 | + cerr << a << "L"; |
| 28 | +} |
| 29 | +static void print( string a ) { |
| 30 | + cerr << '"' << a << '"'; |
| 31 | +} |
| 32 | +template<typename T> void print( vector<T> a ) { |
| 33 | + cerr << "{"; |
| 34 | + for ( int i = 0 ; i != a.size() ; i++ ) { |
| 35 | + if ( i != 0 ) cerr << ", "; |
| 36 | + print( a[i] ); |
| 37 | + } |
| 38 | + cerr << "}" << endl; |
| 39 | +} |
| 40 | +template<typename T> void eq( int n, T have, T need ) { |
| 41 | + if ( have == need ) { |
| 42 | + cerr << "Case " << n << " passed." << endl; |
| 43 | + } else { |
| 44 | + cerr << "Case " << n << " failed: expected "; |
| 45 | + print( need ); |
| 46 | + cerr << " received "; |
| 47 | + print( have ); |
| 48 | + cerr << "." << endl; |
| 49 | + } |
| 50 | +} |
| 51 | +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { |
| 52 | + if( have.size() != need.size() ) { |
| 53 | + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements."; |
| 54 | + print( have ); |
| 55 | + print( need ); |
| 56 | + return; |
| 57 | + } |
| 58 | + for( int i= 0; i < have.size(); i++ ) { |
| 59 | + if( have[i] != need[i] ) { |
| 60 | + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "."; |
| 61 | + print( have ); |
| 62 | + print( need ); |
| 63 | + return; |
| 64 | + } |
| 65 | + } |
| 66 | + cerr << "Case " << n << " passed." << endl; |
| 67 | +} |
| 68 | +static void eq( int n, string have, string need ) { |
| 69 | + if ( have == need ) { |
| 70 | + cerr << "Case " << n << " passed." << endl; |
| 71 | + } else { |
| 72 | + cerr << "Case " << n << " failed: expected "; |
| 73 | + print( need ); |
| 74 | + cerr << " received "; |
| 75 | + print( have ); |
| 76 | + cerr << "." << endl; |
| 77 | + } |
| 78 | +} |
| 79 | +// END CUT HERE |
| 80 | + |
| 81 | +#define REP(i,n) for(int i=0;i<(n);++i) |
| 82 | +#define FOR(i,a,b) for(int i=(a);i<=(b);++i) |
| 83 | +#define RFOR(i,a,b) for(int i=(a);i>=(b);--i) |
| 84 | +#define FOREACH(it,c) for(typeof((c).begin())it=(c).begin();it!=(c).end();++it) |
| 85 | +#define CLR(x) memset((x),0,sizeof((x))) |
| 86 | +typedef long long LL; |
| 87 | +typedef vector<int> VI; |
| 88 | +typedef vector<string> VS; |
| 89 | + |
| 90 | +// BEGIN CUT HERE |
| 91 | +vector<string> split( const string& s, const string& delim =" " ) { |
| 92 | + vector<string> res; |
| 93 | + string t; |
| 94 | + for ( int i = 0 ; i != s.size() ; i++ ) { |
| 95 | + if ( delim.find( s[i] ) != string::npos ) { |
| 96 | + if ( !t.empty() ) { |
| 97 | + res.push_back( t ); |
| 98 | + t = ""; |
| 99 | + } |
| 100 | + } else { |
| 101 | + t += s[i]; |
| 102 | + } |
| 103 | + } |
| 104 | + if ( !t.empty() ) { |
| 105 | + res.push_back(t); |
| 106 | + } |
| 107 | + return res; |
| 108 | +} |
| 109 | + |
| 110 | +vector<int> splitInt( const string& s, const string& delim =" " ) { |
| 111 | + vector<string> tok = split( s, delim ); |
| 112 | + vector<int> res; |
| 113 | + for ( int i = 0 ; i != tok.size(); i++ ) |
| 114 | + res.push_back( atoi( tok[i].c_str() ) ); |
| 115 | + return res; |
| 116 | +} |
| 117 | +// END CUT HERE |
| 118 | + |
| 119 | +// BEGIN CUT HERE |
| 120 | +int s2i(string s) { |
| 121 | + stringstream ss; |
| 122 | + ss << s; |
| 123 | + int res; |
| 124 | + ss >> res; |
| 125 | + return res; |
| 126 | +} |
| 127 | + |
| 128 | +string i2s(int n) { |
| 129 | + stringstream ss; |
| 130 | + ss << n; |
| 131 | + string res; |
| 132 | + ss >> res; |
| 133 | + return res; |
| 134 | +} |
| 135 | +// END CUT HERE |
| 136 | +#define MAXN 105 |
| 137 | +#define _clr(x) memset(x,0xff,sizeof(int)*MAXN) |
| 138 | + |
| 139 | +int match1[MAXN], match2[MAXN]; |
| 140 | + |
| 141 | +int hungary(int m,int n,int mat[][MAXN],int* match1,int* match2){ |
| 142 | + int s[MAXN],t[MAXN],p,q,ret=0,i,j,k; |
| 143 | + for (_clr(match1),_clr(match2),i=0;i<m;ret+=(match1[i++]>=0)) |
| 144 | + for (_clr(t),s[p=q=0]=i;p<=q&&match1[i]<0;p++) |
| 145 | + for (k=s[p],j=0;j<n&&match1[i]<0;j++) |
| 146 | + if (mat[k][j]&&t[j]<0){ |
| 147 | + s[++q]=match2[j],t[j]=k; |
| 148 | + if (s[q]<0) |
| 149 | + for (p=j;p>=0;j=p) |
| 150 | + match2[j]=k=t[j],p=match1[k],match1[k]=j; |
| 151 | + } |
| 152 | + return ret; |
| 153 | +} |
| 154 | + |
| 155 | +vector<int> children[MAXN]; |
| 156 | +int P; |
| 157 | +map<int, int> mm[MAXN]; |
| 158 | + |
| 159 | +// for memoization, we use an associative array. mem[i][j] describes the |
| 160 | +// entry for node i. Where j is the number of sub-interval of length w |
| 161 | +// the number of sub-interval can be pretty large, hence why we do not |
| 162 | +// use a simple array. Note each mem[i] will need at most P values in the map<int,int> |
| 163 | + |
| 164 | +bool doable(int i, int x, int j) { |
| 165 | + // interval length w = 1 / x |
| 166 | + // Can we put node i in interval [j/x , (j+1)/x] ? |
| 167 | + |
| 168 | + if (mm[i].count(j) != 0) return mm[i][j]; |
| 169 | + |
| 170 | + // A leaf. |
| 171 | + // make sure there is no integer k such that k/p is strictly inside interval |
| 172 | + bool res = false; |
| 173 | + if (children[i].size() == 0) { |
| 174 | + res = true; |
| 175 | + FOR(k,1,P-1) { |
| 176 | + // Check if ( j/x < k/P < (j+1)/x): |
| 177 | + if ((P * j < x * k) && (x * k < P * (j + 1))) res = false; |
| 178 | + } |
| 179 | + } else { |
| 180 | + int g = children[i].size(); |
| 181 | + int can[MAXN][MAXN]; |
| 182 | + //memset(can, 1, sizeof(can)); |
| 183 | + REP(b,g) { |
| 184 | + bool nontrivial = false; |
| 185 | + |
| 186 | + // We want to know if a k exists such that k/P is between |
| 187 | + // the b-th sub-interval |
| 188 | + FOR(k,1,P-1) { |
| 189 | + // the b-th sub-interval starts at j/x + b /gx |
| 190 | + // and finishes at j/x + (b+1)/gx |
| 191 | + // (j/x is the start of node i's interval) |
| 192 | + |
| 193 | + // Check if ( j/x + b/gx < k/P < j/x + (b+1)/gx |
| 194 | + // j*g*P + b*P <k*g*x< j*g*P+(b+1)*P |
| 195 | + if ((g * j * P + b * P < g * k * x) && (g * k * x < g * j * P + (b + 1) * P)) nontrivial = true; |
| 196 | + } |
| 197 | + |
| 198 | + if (nontrivial) { |
| 199 | + REP(a,g) { |
| 200 | + can[a][b] = doable(children[i][a], x * g, j * g + b); |
| 201 | + } |
| 202 | + } else { |
| 203 | + REP(a,g) can[a][b] = true; |
| 204 | + } |
| 205 | + } |
| 206 | + int t = hungary(g, g, can, match1, match2); |
| 207 | + res = (t == g); |
| 208 | + } |
| 209 | + |
| 210 | + return (mm[i][j] = res); |
| 211 | +} |
| 212 | + |
| 213 | +class LotteryTree { |
| 214 | +public: |
| 215 | + string isFairTree(vector <int> tree, int _P) { |
| 216 | + P = _P; |
| 217 | + REP(i,MAXN) { |
| 218 | + children[i].clear(); |
| 219 | + mm[i].clear(); |
| 220 | + } |
| 221 | + REP(i,tree.size()) children[tree[i]].push_back(i + 1); |
| 222 | + return doable(0, 1, 0) ? "YES" : "NO"; |
| 223 | + } |
| 224 | +}; |
| 225 | +// BEGIN CUT HERE |
| 226 | +int main() { |
| 227 | + { |
| 228 | + int treeARRAY[] = {0, 0, 0}; |
| 229 | + vector <int> tree( treeARRAY, treeARRAY+ARRSIZE(treeARRAY) ); |
| 230 | + LotteryTree theObject; |
| 231 | + eq(0, theObject.isFairTree(tree, 3),"YES"); |
| 232 | + } |
| 233 | + { |
| 234 | + int treeARRAY[] = {0, 0, 0, 1, 1, 2, 2, 3, 3}; |
| 235 | + vector <int> tree( treeARRAY, treeARRAY+ARRSIZE(treeARRAY) ); |
| 236 | + LotteryTree theObject; |
| 237 | + eq(1, theObject.isFairTree(tree, 2),"YES"); |
| 238 | + } |
| 239 | + { |
| 240 | + int treeARRAY[] = {0, 0, 1, 1, 2, 2, 4, 4, 4}; |
| 241 | + vector <int> tree( treeARRAY, treeARRAY+ARRSIZE(treeARRAY) ); |
| 242 | + LotteryTree theObject; |
| 243 | + eq(2, theObject.isFairTree(tree, 3),"NO"); |
| 244 | + } |
| 245 | + { |
| 246 | + int treeARRAY[] = {0, 0, 1, 1, 1, 3, 3, 3}; |
| 247 | + vector <int> tree( treeARRAY, treeARRAY+ARRSIZE(treeARRAY) ); |
| 248 | + LotteryTree theObject; |
| 249 | + eq(3, theObject.isFairTree(tree, 3),"NO"); |
| 250 | + } |
| 251 | + { |
| 252 | + int treeARRAY[] = {0, 0, 0, 3, 0, 0, 3, 6, 3, 1, 0, 2, 0, 4, 6, 15, 1, 15, 11, 11, 1, 4, 11, 2, 11, 2, 6} |
| 253 | + ; |
| 254 | + vector <int> tree( treeARRAY, treeARRAY+ARRSIZE(treeARRAY) ); |
| 255 | + LotteryTree theObject; |
| 256 | + eq(4, theObject.isFairTree(tree, 6),"YES"); |
| 257 | + } |
| 258 | + { |
| 259 | + int treeARRAY[] = {0, 1, 2, 3, 1, 1, 4, 4, 0, 1, 6, 9, 1, 12, 9, 2, 4, 8, 6, 13, 8, 5, 11, 12, 17, |
| 260 | + 19, 13, 9, 3, 24, 30, 29, 28, 28, 11, 27, 2, 26, 6, 14, 8, 26, 15, 25, 33, 38, |
| 261 | + 1, 24, 15, 43, 3, 39, 26, 8, 13, 50, 28, 8, 6, 27, 8, 38, 27, 50, 17, 50, 25, |
| 262 | + 40, 7, 29, 22, 40, 2, 24, 22, 30, 33, 40, 19, 14, 26, 39, 5, 43, 7, 4}; |
| 263 | + vector <int> tree( treeARRAY, treeARRAY+ARRSIZE(treeARRAY) ); |
| 264 | + LotteryTree theObject; |
| 265 | + eq(5, theObject.isFairTree(tree, 9),"NO"); |
| 266 | + } |
| 267 | + { |
| 268 | + int treeARRAY[] = {0, 1, 0, 0, 4, 0, 2, 2, 0, 2, 6, 1, 3, 6, 5, 9, 11, 13, 1, 10, 14, 4, 7, 21, |
| 269 | + 16, 8, 25, 4, 5, 22, 25, 14, 12, 11, 12, 26, 21, 8, 2, 38, 3, 5, 4, 38, 27, |
| 270 | + 35, 38, 30, 38, 9, 16, 36, 6, 10, 7, 27, 30, 33, 17, 26, 17, 10, 35, 10, 38, |
| 271 | + 41, 15, 9, 3, 27, 8, 15, 38, 22, 41, 33, 33, 36, 30, 13, 18, 22, 18}; |
| 272 | + vector <int> tree( treeARRAY, treeARRAY+ARRSIZE(treeARRAY) ); |
| 273 | + LotteryTree theObject; |
| 274 | + eq(6, theObject.isFairTree(tree, 12),"YES"); |
| 275 | + } |
| 276 | + { |
| 277 | + int treeARRAY[] = {0, 0, 2, 3, 4, 3, 2, 1, 8, 6, 8, 8, 2, 7, 14, 2, 8, 1, 11, 11, 12, 16, 12, |
| 278 | + 19, 20, 13, 7, 12, 26, 11, 18, 19, 18, 20, 4, 9, 1, 1, 6, 16, 1, 35, 27, 24, |
| 279 | + 37, 30, 36, 41, 32, 36, 8, 2, 6, 14, 41, 1, 35, 6, 30, 16, 12, 2, 35, 25, 50, |
| 280 | + 13, 1, 24, 8, 32, 26, 50, 9, 19, 9, 20, 26, 27, 6, 12, 25, 9, 37, 37, 9} ; |
| 281 | + vector <int> tree( treeARRAY, treeARRAY+ARRSIZE(treeARRAY) ); |
| 282 | + LotteryTree theObject; |
| 283 | + eq(7, theObject.isFairTree(tree, 7),"NO"); |
| 284 | + } |
| 285 | + { |
| 286 | + int treeARRAY[] = {0, 0, 1, 0, 2, 3, 0, 0, 8, 5, 7, 5, 2, 12, 12, 14, 14, 13, 8, 2, 1, 7, 18, |
| 287 | + 16, 8, 24, 18, 2, 24, 3, 11, 5, 24, 4, 34, 6, 31, 13, 38, 19, 4, 3, 22, 3, |
| 288 | + 11, 12, 21, 34, 41, 8, 19, 4, 13, 29, 33, 8, 14, 50, 18, 45, 16, 50, 44, 50, |
| 289 | + 38, 5, 43, 31, 29, 7, 6, 45, 38, 4, 19, 41, 50, 21, 44, 41, 43, 22, 33, 6, 8} |
| 290 | + ; |
| 291 | + vector <int> tree( treeARRAY, treeARRAY+ARRSIZE(treeARRAY) ); |
| 292 | + LotteryTree theObject; |
| 293 | + eq(8, theObject.isFairTree(tree, 12),"YES"); |
| 294 | + } |
| 295 | + return 0; |
| 296 | +} |
| 297 | +// END CUT HERE |
0 commit comments