比赛 |
NOIP水题赛 |
评测结果 |
AAAAAAAAAA |
题目名称 |
AAAD(无题面) |
最终得分 |
100 |
用户昵称 |
胡嘉兴 |
运行时间 |
5.180 s |
代码语言 |
C++ |
内存使用 |
99.49 MiB |
提交时间 |
2018-10-30 18:35:51 |
显示代码纯文本
#include<bits/stdc++.h>
#define ll long long
#define lowbit(x) (x&-x)
using namespace std;
const int N = 2e6+7, inf = 1e9+7;
queue<int> q;
int val[N], dis[N][2], vis[N];
vector<int> vec[N], gap[N], book[N];
void spfa()
{
q.push(1);
memset(dis, 0x3f, sizeof(dis));
dis[1][0] = 0;
while(!q.empty())
{
int u = q.front();
/*for(int i = val[u]; i >= 0; i -= lowbit(i))
{
int c = (val[u]^lowbit(i));
for(int j = 0; j < book[c].size(); j++)
{
int v = book[c][j];
if(u!=v)
{
for(int j = 0; j < 2; j++)
{
if(dis[v][1]>dis[u][j]+(1-j))
{
dis[v][1] = dis[u][j]+(1-j);
if(!vis[v])
{
q.push(v);
vis[v] = 1;
}
}
}
}
}
if(!i)
{
break;
}
}*/
for(int i = 0; i < gap[u].size(); i++)
{
int v = gap[u][i];
if(u!=v)
{
for(int j = 0; j < 2; j++)
{
if(dis[v][1]>dis[u][j]+1-j)
{
dis[v][1] = dis[u][j]+1-j;
if(!vis[v])
{
q.push(v);
vis[v] = 1;
}
}
}
}
}
for(int i = 0; i < vec[u].size(); i++)
{
int v = vec[u][i];
if(v!=u)
{
for(int j = 0; j < 2; j++)
{
if(dis[v][0]>dis[u][j]+1)
{
dis[v][0] = dis[u][j]+1;
if(!vis[v])
{
q.push(v);
vis[v] = 1;
}
}
}
}
}
q.pop();
vis[u] = 0;
}
return;
}
int main()
{
int n, m, tot;
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
freopen("AAAD.in", "r", stdin);
freopen("AAAD.out", "w", stdout);
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
{
scanf("%d", &val[i]);
book[val[i]].push_back(i);
}
tot = n;
for(int i = 1; i < (1<<20); i++)
{
if(!book[i].size())
{
val[++n] = i;
book[i].push_back(n);
}
else
{
for(int j = 1; j < book[i].size(); j++)
{
int u = book[i][j-1], v = book[i][j];
gap[u].push_back(v);
gap[v].push_back(u);
}
}
}
for(int i = 1; i < (1<<20); i++)
{
for(int j = i; j; j ^= lowbit(j))
{
int v = (i^lowbit(j));
if(book[v].size())
{
gap[book[i].front()].push_back(book[v].front());
}
}
}
for(int i = 1; i <= m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
vec[u].push_back(v);
}
spfa();
for(int i = 1; i <= tot; i++)
{
int res = min(dis[i][0], dis[i][1]);
printf("%d\n", res>inf?-1:res);
}
return 0;
}