QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#149889 | #4934. Forbidden Card | MegaSam | WA | 1ms | 9924kb | C++14 | 2.6kb | 2023-08-25 14:29:31 | 2023-08-25 14:29:34 |
Judging History
answer
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int N=1e5+7,M=1e6+7;
int n,m,cnt,pos;
int a[N],b[N],ans[N],f[M],dep[M],fa[M];
vector<int> ve;
bool vis[M],is[M];
int find(int k){ return k==f[k]?k:f[k]=find(f[k]); }
void work2(){
for(int i=1;i<=m;i++) vis[i]=0;
is[0]=1;
bool flag=0;
int cc=0;
for(int i=1;i<=n;i++){
cc=i;
if(vis[a[i]]&& vis[b[i]]) {flag=1;break;}
else if(vis[a[i]]&&(!vis[b[i]])) {
int tmp=b[i];
while(!is[tmp]){
ans[i]++;
is[tmp]=1;
tmp=fa[tmp];
}
}else if((!vis[a[i]])&&vis[b[i]]){
int tmp=a[i];
while(!is[tmp]){
ans[i]++;
is[tmp]=1;
tmp=fa[tmp];
}
}else{
int ra=find(a[i]),rb=find(b[i]);
if(ra==rb){
int tmp=dep[a[i]]>dep[b[i]]?b[i]:a[i];
while(!is[tmp]){
ans[i]++;
is[tmp]=1;
tmp=fa[tmp];
}
}
}
if(!vis[a[i]]) vis[a[i]]=1;
else if(vis[a[i]] &&!vis[b[i]]) vis[b[i]]=1;
if(find(a[i])!=find(b[i])){
f[b[i]]=a[i];
fa[b[i]]=a[i];
dep[b[i]]=dep[a[i]]+1;
}
}
// int sum=0;
// for(int i=1;i<=n;i++) sum+=ans[i];
// printf("%d\n%d",sum,cc);
if(!flag){
for(int i=1;i<=n;i++){
if(vis[a[i]]&& vis[b[i]]) {break;}
else if(vis[a[i]]&&(!vis[b[i]])) {
int tmp=b[i];
while(!is[tmp]){
ans[i]++;
is[tmp]=1;
tmp=fa[tmp];
}
}else if((!vis[a[i]])&&vis[b[i]]){
int tmp=a[i];
while(!is[tmp]){
ans[i]++;
is[tmp]=1;
tmp=fa[tmp];
}
}else{
int ra=find(a[i]),rb=find(b[i]);
if(ra==rb){
int tmp=dep[a[i]]>dep[b[i]]?b[i]:a[i];
while(!is[tmp]){
ans[i]++;
is[tmp]=1;
tmp=fa[tmp];
}
}
}
if(!vis[a[i]]) vis[a[i]]=1;
else if(!vis[b[i]]) vis[b[i]]=1;
if(find(a[i])!=find(b[i])){
f[b[i]]=a[i];
fa[b[i]]=a[i];
dep[b[i]]=dep[a[i]]+1;
}
}
}
int sum=0;
for(int i=1;i<=n;i++)sum+=ans[i];
ans[pos]=m-sum;
for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
// printf("%d\n",sum);
}
int main(){
// freopen("marketplace.in","r",stdin);
// freopen("marketplace.out","w",stdout);
scanf("%d%d",&n,&m); cnt=m;
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i],&b[i]);
}
for(int i=1;i<=m;i++) vis[i]=0,f[i]=i;
for(int i=1;i<=n;i++){
if(vis[a[i]] && vis[b[i]]){ pos=i;break;}
else if(!vis[a[i]]) vis[a[i]]=1;
else if(!vis[b[i]]) vis[b[i]]=1;
}
if(!pos){
for(int i=1;i<=n;i++){
if(vis[a[i]] && vis[b[i]]){ pos=i;break;}
else if(!vis[a[i]]) vis[a[i]]=1;
else if(!vis[b[i]]) vis[b[i]]=1;
}
}
// printf("%d %d\n",pos,cnt);
if(!pos) pos=1;
// ans[pos]=cnt;
// if(n<=2000) work1();
// else
work2();
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 9924kb
input:
3 6 1 2 2 4 4 2
output:
3 0 3
result:
ok 3 lines
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 7704kb
input:
4 10 1 5 2 6 3 7 4 8
output:
2 2 2 2
result:
wrong answer 1st lines differ - expected: '4', found: '2'