QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#769772 | #2683. British Menu | yinpeichu2021 | WA | 3ms | 18664kb | C++14 | 2.6kb | 2024-11-21 19:21:10 | 2024-11-21 19:21:10 |
Judging History
answer
#include<bits/stdc++.h>
// #define MOD 1000000007
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
#define MAXN 300005
int head[MAXN],ver[MAXN*4],nextt[MAXN*4],tot;
void add(int x,int y){
ver[++tot]=y;
nextt[tot]=head[x];
head[x]=tot;
}
int n,m,ru[MAXN],f[MAXN],ans;
int dfn[MAXN],low[MAXN],num;
int st[MAXN],top,col[MAXN],sum[MAXN],cnt;
vector<int>scc[MAXN];
void tarjan(int x){
dfn[x]=low[x]=++num;
st[++top]=x;
for(int i=head[x];i;i=nextt[i]){
int v=ver[i];
if(!dfn[v]){
tarjan(v);
low[x]=min(low[x],low[v]);
}else if(!col[v])low[x]=min(low[x],dfn[v]);
}
if(low[x]==dfn[x]){
col[x]=++cnt,sum[cnt]++;
scc[cnt].push_back(x);
while(st[top]!=x){
sum[cnt]++,col[st[top]]=cnt;
scc[cnt].push_back(st[top]);
top--;
}
top--;
}
}
bool vis[MAXN],fl[MAXN];
map<int,map<int,int> >mp;
void dfs(int x,int s,int c,int fr){
vis[x]=1;
mp[fr][x]=max(mp[fr][x],s);
for(int i=head[x];i;i=nextt[i]){
int v=ver[i];
if(col[v]!=c||vis[v])continue;
dfs(v,s+1,c,fr);
}
vis[x]=0;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
ru[y]++;
add(x,y);
}
for(int i=1;i<=n;i++)
add(i,n+1);
cnt=n+1;
for(int i=1;i<=n;i++)
if(!dfn[i])tarjan(i);
for(int i=1;i<=n;i++)
for(int j=head[i];j;j=nextt[j]){
int v=ver[j];
if(col[v]&&col[v]!=col[i])ru[col[v]]++;
}
for(int i=1;i<=cnt;i++)
for(int x:scc[i])dfs(x,0,i,x);
for(int i=1;i<=n;i++)f[i]=1;
queue<int>q;
for(int i=1;i<=n;i++)
if(ru[i]==0&&!col[i])q.push(i);
for(int i=n+2;i<=cnt;i++)if(ru[i]==0)
for(int j:scc[i])q.push(j);
while(!q.empty()){
int x=q.front();q.pop();
// cout<<x<<' ';
if(!col[x]){
// cout<<"0\n";
for(int i=head[x];i;i=nextt[i]){
int v=ver[i];
// cout<<x<<"->"<<v<<'\n';
f[v]=max(f[v],f[x]+1);
if(!col[v]&&--ru[v]==0)q.push(v);
if(col[v]&&--ru[col[v]]==0)
for(int j:scc[col[v]])q.push(j);
}
}else{
// cout<<"1\n";
for(int b:scc[col[x]]){
for(int i=head[b];i;i=nextt[i]){
int v=ver[i];
if(col[v]==col[x])continue;
for(int a:scc[col[x]])
f[v]=max(f[v],f[a]+mp[a][b]+1);//,cout<<a<<"->"<<b<<"->"<<v<<'\n';
if(!col[v]&&--ru[v]==0)q.push(v);
if(col[v]&&--ru[col[v]]==0)
for(int j:scc[col[v]])q.push(j);
}
}
}
// cout<<'\n';
}
cout<<f[n+1]-1;
// for(int i=1;i<=n+1;i++)ans=max(ans,f[i]),cout<<f[i]<<' ';
// cout<<'\n';
// cout<<ans;
// cout<<'\n';
// for(int i=1;i<=n;i++,cout<<'\n')
// for(int j=1;j<=n;j++)
// cout<<mp[i][j]<<' ';
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 18140kb
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: 17984kb
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: 0ms
memory: 17976kb
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: 0ms
memory: 15496kb
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: 3ms
memory: 16920kb
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: 0
Accepted
time: 0ms
memory: 15116kb
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:
9
result:
ok single line: '9'
Test #7:
score: 0
Accepted
time: 0ms
memory: 17972kb
input:
12 11 11 10 12 10 8 12 9 12 5 7 1 6 8 11 9 11 7 9 7 11 2 9
output:
5
result:
ok single line: '5'
Test #8:
score: 0
Accepted
time: 0ms
memory: 18192kb
input:
7 7 1 2 2 3 3 4 4 5 5 6 6 2 4 7
output:
6
result:
ok single line: '6'
Test #9:
score: 0
Accepted
time: 2ms
memory: 18348kb
input:
9 9 1 2 2 3 3 4 4 5 5 6 6 2 4 7 8 1 9 8
output:
8
result:
ok single line: '8'
Test #10:
score: 0
Accepted
time: 3ms
memory: 16968kb
input:
7 9 1 2 2 3 3 4 4 5 5 6 6 2 4 7 6 4 3 5
output:
7
result:
ok single line: '7'
Test #11:
score: 0
Accepted
time: 0ms
memory: 18664kb
input:
9 9 1 2 2 3 3 4 4 5 5 6 6 4 4 7 7 8 8 9
output:
7
result:
ok single line: '7'
Test #12:
score: -100
Wrong Answer
time: 0ms
memory: 18232kb
input:
100 600 1 81 1 48 1 64 1 74 1 30 1 58 1 53 1 46 1 76 1 66 1 51 1 68 1 99 1 71 1 41 1 44 1 47 1 19 3 54 4 83 4 33 4 63 4 83 4 29 5 18 5 80 5 49 5 60 5 11 5 78 5 62 5 89 5 45 6 40 6 50 6 21 6 52 6 97 6 49 6 98 6 5 6 43 6 46 6 2 6 19 6 81 7 92 7 34 7 97 7 63 7 2 8 97 9 87 9 3 9 2 10 3 10 90 12 37 12 88...
output:
35
result:
wrong answer 1st lines differ - expected: '90', found: '35'