# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
169251 | arnold518 | Paths (BOI18_paths) | C++14 | 1871 ms | 704472 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 3e5;
const int MAXK = 5;
int N, M, K, KK, A[MAXN+10];
vector<int> adj[MAXN+10], adj2[MAXN*40+10], revadj2[MAXN*40+10];
ll dp[MAXN*40+10], ans;
vector<int> S;
bool vis[MAXN*40+10];
void dfs(int now)
{
vis[now]=true;
for(int nxt : adj2[now])
{
if(vis[nxt]) continue;
dfs(nxt);
}
S.push_back(now);
}
int main()
{
int i, j, k;
scanf("%d%d%d", &N, &M, &K); KK=(1<<K);
for(i=1; i<=N; i++) scanf("%d", &A[i]), A[i]--;
for(i=1; i<=M; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for(i=1; i<=N; i++) for(j=0; j<(1<<K); j++)
{
if((j&(1<<A[i]))==0) continue;
int now=(i<<K)+j;
for(auto to : adj[i])
{
if(j&(1<<A[to])) continue;
int nxt=(to<<K)+(j|(1<<A[to]));
adj2[now].push_back(nxt);
revadj2[nxt].push_back(now);
/*
printf("%d ", now>>K);
for(k=0; k<K; k++) printf("%d", (bool)(now&(1<<k)));
printf(" / ");
printf("%d ", nxt>>K);
for(k=0; k<K; k++) printf("%d", (bool)(nxt&(1<<k)));
printf("\n");*/
}
}
for(i=1; i<=N; i++) dfs((i<<K)+(1<<A[i]));
reverse(S.begin(), S.end());
for(auto now : S)
{
if(now==((now>>K)<<K)+(1<<A[now>>K])) dp[now]=1;
for(auto nxt : revadj2[now]) dp[now]+=dp[nxt];
ans+=dp[now];
}
ans-=N;
printf("%lld", ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |