QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#623956 | #8943. Challenge Matrix Multiplication | Coffins | TL | 0ms | 35208kb | C++14 | 1.5kb | 2024-10-09 14:28:44 | 2024-10-09 14:28:45 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
using pii=pair<int,int>;
int n,m,ct=0;vector<int> edge[N];
int deg[N];set<pii> s;
int fr[N];vector<int> vc;
bool vs[N];int f[N];
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y;cin>>x>>y;
edge[x].push_back(y);
deg[x]++,deg[y]--;
}for(int i=1;i<=n;i++)
ct+=abs(deg[i]),s.insert({-deg[i],i});
while(1)
{
int tot=0;for(int i=1;i<=n;i++)
tot+=!f[i];if(!tot)break;
int u=s.begin()->second,v=u;
for(int i=1;i<=n;i++)fr[i]=0;
queue<int> q;q.push(u);vc.clear();
while(!q.empty())
{
int u=q.front();q.pop();
if(deg[u]<0){v=u;break;}
for(auto v:edge[u])
if(!fr[v])fr[v]=u,q.push(v);
}s.erase({-deg[u],u}),deg[u]--;
s.insert({-deg[u],u});
s.erase({-deg[v],v}),deg[v]++;
s.insert({-deg[v],v});
while(v!=u)vc.push_back(v),
v=fr[v];vc.push_back(u);
int sz=vc.size(),ct=0;
while(!q.empty())q.pop();
for(int i=1;i<=n;i++)vs[i]=0;
for(int i=0;i<sz;i++)
{
q.push(vc[i]);vs[vc[i]]=1;
while(!q.empty())
{
int u=q.front();
ct++;q.pop();
for(auto v:edge[u])
if(!vs[v])q.push(v),vs[v]=1;
}f[vc[i]]=ct;
}
}for(int i=1;i<=n;i++)cout<<f[i]<<' ';
cout<<'\n';return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 35208kb
input:
4 6 1 3 2 3 2 4 1 2 1 3 1 3
output:
4 3 1 1
result:
ok 4 number(s): "4 3 1 1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 33388kb
input:
5 7 1 4 1 5 1 2 2 4 3 4 2 5 1 4
output:
4 3 2 1 1
result:
ok 5 number(s): "4 3 2 1 1"
Test #3:
score: -100
Time Limit Exceeded
input:
100 900 89 90 34 35 45 46 97 98 41 42 59 61 19 20 25 29 2 3 28 29 54 55 77 78 69 74 60 61 43 44 13 14 92 93 65 66 68 69 72 73 78 81 54 56 55 60 14 15 9 10 92 93 10 11 18 19 16 17 97 98 76 77 39 40 71 72 7 8 63 64 7 8 16 24 13 24 83 84 90 91 1 4 4 5 96 97 81 82 91 92 80 81 66 67 19 20 3 4 9 10 47 48 ...