QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#276554 | #6679. Not Another Path Query Problem | 13475040098 | RE | 2ms | 11644kb | C++14 | 1.9kb | 2023-12-05 22:42:15 | 2023-12-05 22:42:15 |
Judging History
answer
#include<iostream>
#include<vector>
#include<queue>
#include<bitset>
using namespace std;
const int MN = 1e5+5;
const int MQ = 5e5+5;
vector<int> e[MN];
vector<long long> v[MN];
int que[MQ][2];
int n,m,q;
long long V;
int ans[MQ];
int vis[MN];//记录该点属于哪个并查集中(在bfs中使用)
long long lowbit(long long x)
{
return x&-x;
}
void bfs(int st,long long t)
{
queue<int> s;
s.push(st);
vis[st] = st;
while(!s.empty())
{
int top = s.front();
s.pop();
for(int i=0;i<e[top].size();i++)
{
int point = e[top][i];
long long val = v[top][i];
if(vis[point]||(val&t)!=t)//该点已经在并查集里面了(剪枝,并且因为是图,防止产生回路死循环);或者该点不符合加入该路径的条件
continue;
s.push(point);
vis[point] = st;//该点加入并查集
}
}
}
void gao(long long t)
{
for(int i=1;i<=n;i++)//初始化为0
vis[i] = 0;
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
bfs(i,t); //以i为起点进行bfs,所有能到达的点的vis记为i,表示同属于i的并查集内
}
for(int i=0;i<q;i++)
{
if(vis[que[i][0]]==vis[que[i][1]])//查询的起点和终点属于同一个并查集,说明在该路径下两点是连通的,说明至少有一条路径可以联通两点
ans[i] = 1;
}
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m>>q>>V;
for(int i=0;i<m;i++)
{
int x,y,w;
cin>>x>>y>>w;
e[x].push_back(y);v[x].push_back(w);
e[y].push_back(x);v[y].push_back(w);
}
for(int i=0;i<q;i++)
{
int x,y;
cin>>x>>y;
que[i][0]=x;que[i][1]=y;
}
if(V==0)
gao(0);
else
for(long long i=V;i<(1LL<<60);i+=lowbit(i))//lowbit操作从最后一个一开始向下取。加上lowbit操作为让最后一个0(这个0后面至少有一个1)变成1,并且其后面全都变成0
gao(i);//比如001010->001100->010000->100000
for(int i=0;i<q;i++)
cout<<(ans[i]==1?"Yes":"No")<<endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11004kb
input:
9 8 4 5 1 2 8 1 3 7 2 4 1 3 4 14 2 5 9 4 5 7 5 6 6 3 7 15 1 6 2 7 7 6 1 8
output:
Yes No Yes No
result:
ok 4 token(s): yes count is 2, no count is 2
Test #2:
score: 0
Accepted
time: 0ms
memory: 11644kb
input:
3 4 1 4 1 2 3 1 2 5 2 3 2 2 3 6 1 3
output:
Yes
result:
ok YES
Test #3:
score: -100
Runtime Error
input:
100 2000 50000 0 32 52 69658009083393280 26 38 868250171554967916 87 32 743903879320440454 22 15 19782587273744714 57 98 845866434191429143 42 95 1145336983294966993 67 40 1036117659380117375 46 24 265457274847122243 63 44 438254608190938148 28 23 992625102587165494 57 87 558124114385470345 6 17 535...