Submission #98748

#TimeUsernameProblemLanguageResultExecution timeMemory
98748luckyboyUzastopni (COCI15_uzastopni)C++14
128 / 160
8 ms1280 KiB
/**Lucky Boy**/ #include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define FORD(i, a, b) for (int i = (a); i >= (b); --i) #define pb push_back #define mp make_pair #define F first #define S second #define maxc 1000000007 #define maxn 10004 #define maxm 101 #define pii pair <int,int> #define Task "UZASTOPNI" template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;} template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);} template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');} template <typename T> inline void writeln(T x){write(x);putchar('\n');} using namespace std; int n,val[maxn]; vector <int> a[maxn]; bitset <maxm> l[maxn],r[maxn],temp; bool dd[maxm]; bool comp(int x,int y) { return val[x] < val[y]; } void Dfs(int u) { dd[val[u]] = 1; l[u].set(val[u],1); r[u].set(val[u],1); for (int v : a[u]) if (!dd[val[v]]) { Dfs(v); } for (int v : a[u]) { if (dd[val[v]] || val[v] < val[u]) continue; temp = 0; temp = r[u]; temp >>= (100 - val[v]); temp &= l[v]; if (temp.any()) r[u].set(val[v],1); if (r[u][val[v] - 1]) { r[u] |= r[v]; } } reverse(a[u].begin(),a[u].end()); for (int v : a[u]) { if (dd[val[v]] || val[v] > val[u]) continue; temp = 0; temp = l[u]; temp >>= (100 - val[v]); temp &= r[v]; if (temp.any()) l[u].set(val[v],1); if (l[u][val[v] + 1]) { l[u] |= l[v]; } } dd[val[u]] = 0; } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //freopen(Task".inp", "r",stdin); //freopen(Task".out", "w",stdout); cin >> n; FOR(i,1,n) cin >> val[i]; FOR(i,2,n) { int x,y; cin >> x >> y; a[x].pb(y); } FOR(i,1,n) sort(a[i].begin(),a[i].end(),comp); Dfs(1); int ll = l[1].count(), rr = r[1].count(); cout << ll * rr; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...