社区讨论
63pts求调
P2783有机化学之神偶尔会做作弊参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @louxta8f
- 此快照首次捕获于
- 2023/11/12 11:50 2 年前
- 此快照最后确认于
- 2023/11/12 14:07 2 年前
rt
CPP#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+1;
struct node{
int ne;
int bi;
}aa[maxn];
int c[maxn],low[maxn],dfn[maxn],num,tot,dcc,d[maxn],f[maxn][22],ans[maxn],tt[maxn];
int n,m;
bool v[maxn];
vector<node> t[maxn];
vector<int> t1[maxn];
map<int,bool> vis[maxn];
queue<int> q;
void tarjan(int x,int fa){
low[x]=dfn[x]=++num;
for(int i=0;i<t[x].size();i++){
int y=t[x][i].ne;
//if(y==fa) continue;
if(!dfn[y]){
tarjan(y,x);
low[x]=min(low[x],low[y]);
if(low[y]>dfn[x]) v[t[x][i].bi]=1;
}else if(y!=fa) low[x]=min(low[x],dfn[y]);
}
}
void dfs(int x){
c[x]=dcc;
for(int i=0;i<t[x].size();i++){
int y=t[x][i].ne;
if(c[y]||v[t[x][i].bi]) continue;
dfs(y);
}
}
void bfs(){
q.push(1),d[1]=1;
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=0;i<t1[x].size();i++){
int y=t1[x][i];
if(d[y]) continue;
d[y]=d[x]+1;
f[y][0]=x;
for(int j=1;j<=tot;j++){
f[y][j]=f[f[y][j-1]][j-1];
}
q.push(y);
}
}
}
int lca(int x,int y){
if(d[x]<d[y]) swap(x,y);
for(int i=tot;i>=0;i--) if(d[x]-(1<<i)>=d[y]) x=f[x][i];
if(x==y) return x;
for(int i=tot;i>=0;i--) if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
return f[x][0];
}
void shuchu(int x)
{
int xx = 0;
for(; x; x>>=1)
{
xx++;
if(x & 1) tt[xx] = 1;
else tt[xx] = 0;
}
for (int i = xx; i >= 1; i-- ) printf("%d",tt[i]);
printf("\n");
}
int main(){
cin>>n>>m;
tot=log(n)/log(2)+1;
for(int a,b,i=1;i<=m;i++){
scanf("%d%d",&a,&b);
if(vis[a][b]==1) continue;
vis[a][b]=1,vis[b][a]=1;
node temp;
temp.ne=b,temp.bi=i;
t[a].push_back(temp);
node temp1;
temp1.ne=a,temp.bi=i;
t[b].push_back(temp1);
}
for(int i=1;i<=n;i++){
if(!dfn[i]) tarjan(i,0);
}
for(int i=1;i<=n;i++){
if(!c[i]){
dcc++;
dfs(i);
}
}
for(int i=1;i<=n;i++){
for(int j=0;j<t[i].size();j++){
int x=t[i][j].ne;
if(c[x]==c[i]) continue;
t1[c[x]].push_back(c[i]);
t1[c[i]].push_back(c[x]);
}
}
bfs();
int kk;
cin>>kk;
for(int a,b,i=1;i<=kk;i++){
cin>>a>>b;
int gg=d[c[a]]+d[c[b]]-2*d[lca(c[a],c[b])]+1;
shuchu(gg);
}
return 0;
}
奇怪的是如果将tarjan中
CPP//if(y==fa) continue;
这一句去掉就会RE
报错还不是越界而是奇怪的提示 求调
回复
共 0 条回复,欢迎继续交流。
正在加载回复...