QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#775007 | #2683. British Menu | zjc5 | WA | 5ms | 25136kb | C++14 | 1.9kb | 2024-11-23 14:29:08 | 2024-11-23 14:29:09 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=100010,M=6;
int n,m,a[N],b[N],dfn[N],low[N],cnt;
int st[N],tp,fa[N],ta[N],ct[N],dis[N][M][M];
int in[N],f[N][M],ans;
bool ins[N],bl[M];
vector<int>v[N];
struct node{
int t,b;
};
vector<node>tar[N][M];
queue<int>q;
void tarjan(int x){
dfn[x]=low[x]=++cnt;
st[++tp]=x;
ins[x]=1;
for(int t:v[x]){
if(!dfn[t]){
tarjan(t);
low[x]=min(low[x],low[t]);
}else if(ins[t])
low[x]=min(low[x],dfn[t]);
}
if(low[x]==dfn[x]){
int y;
while(1){
y=st[tp--];
fa[y]=x;
ins[y]=0;
ta[y]=++ct[x];
if(y==x) break;
}
}
}
void dfs(int st,int now,int d,int bel){
for(int t:v[now])
if(fa[t]==bel&&!bl[ta[t]])
if(d+1>dis[bel][st][ta[t]]){
dis[bel][st][ta[t]]=d+1;
bl[ta[t]]=1;
dfs(st,t,d+1,bel);
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>a[i]>>b[i];
v[a[i]].push_back(b[i]);
}
for(int i=1;i<=n;i++)
if(!dfn[i]) tarjan(i);
for(int i=1;i<=n;i++){
dis[fa[i]][ta[i]][ta[i]]=0;
memset(bl,0,sizeof(bl));
bl[ta[i]]=1;
dfs(ta[i],i,0,fa[i]);
}
for(int i=1;i<=m;i++){
int p=fa[a[i]],q=fa[b[i]];
if(p!=q){
tar[p][ta[a[i]]].push_back({q,ta[b[i]]});
in[q]++;
}
}
for(int i=1;i<=n;i++)
if(ct[i]&&!in[i]){
for(int j=1;j<=ct[i];j++)
for(int k=1;k<=ct[i];k++)
f[i][j]=max(f[i][j],dis[i][k][j]+1);
q.push(i);
}
while(!q.empty()){
int d=q.front();
q.pop();
for(int j=1;j<=ct[d];j++){
int s=f[d][j]+1;
for(node k:tar[d][j]){
for(int p=1;p<=ct[k.t];p++)
f[k.t][p]=max(f[k.t][p],dis[k.t][k.b][p]+s);
in[k.t]--;
if(!in[k.t]) q.push(k.t);
}
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=ct[i];j++)
ans=max(ans,f[i][j]);
cout<<ans;
return 0;
}
/*
7 7
1 2
2 3
3 4
4 5
5 2
4 6
5 7
ans:6
7 8
1 2
2 3
3 1
3 4
4 5
5 3
4 6
6 7
ans:6
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 24684kb
input:
10 6 7 8 4 2 8 6 5 1 4 1 4 5
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 22280kb
input:
10 10 1 3 8 9 6 10 1 2 7 9 10 9 5 1 2 5 8 6 7 8
output:
5
result:
ok single line: '5'
Test #3:
score: 0
Accepted
time: 5ms
memory: 25136kb
input:
10 8 7 6 4 2 10 6 4 5 2 4 3 2 6 10 2 1
output:
4
result:
ok single line: '4'
Test #4:
score: 0
Accepted
time: 3ms
memory: 23792kb
input:
10 19 3 6 9 10 7 9 9 8 8 7 5 6 3 8 6 9 5 9 2 6 6 8 1 4 6 7 6 10 3 9 10 7 4 9 10 8 1 8
output:
6
result:
ok single line: '6'
Test #5:
score: 0
Accepted
time: 0ms
memory: 25076kb
input:
10 19 2 7 8 10 9 8 3 10 8 9 4 5 2 10 1 8 9 6 1 9 4 6 3 2 5 9 5 2 10 7 5 10 7 6 6 9 4 7
output:
8
result:
ok single line: '8'
Test #6:
score: -100
Wrong Answer
time: 5ms
memory: 23672kb
input:
10 18 3 6 2 10 2 5 5 3 4 2 1 5 7 9 10 7 8 6 3 2 4 8 8 9 3 7 7 8 1 4 3 1 5 1 3 9
output:
8
result:
wrong answer 1st lines differ - expected: '9', found: '8'