Submission #98751

#TimeUsernameProblemLanguageResultExecution timeMemory
98751luckyboyUzastopni (COCI15_uzastopni)C++14
160 / 160
9 ms1152 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]; 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; FOR(i,val[u] + 1,val[v]) if (r[u][i-1] && l[v][i]) { r[u].set(val[v],1); break; } 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; FOR(i,val[v],val[u] - 1) if (r[v][i] && l[u][i+1]) { l[u].set(val[v],1); break; } 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...