QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#97708 | #6329. Colorful Graph | Bazoka13 | WA | 3ms | 6036kb | C++14 | 2.2kb | 2023-04-17 23:04:40 | 2023-04-17 23:04:43 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn=7e3+15;
int n,m,sum,tim,top,s;
int p[maxn],head[maxn],sd[maxn],dfn[maxn],low[maxn];
int stac[maxn],vis[maxn];
int h[maxn],in[maxn],dist[maxn];
struct EDGE
{
int to;int next;int from;
}edge[maxn*10],ed[maxn*10];
void add(int x,int y)
{
edge[++sum].next=head[x];
edge[sum].from=x;
edge[sum].to=y;
head[x]=sum;
}
void tarjan(int x)
{
low[x]=dfn[x]=++tim;
stac[++top]=x;vis[x]=1;
for (int i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if (!dfn[v]) {
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if (vis[v])
{
low[x]=min(low[x],low[v]);
}
}
if (dfn[x]==low[x])
{
int y;
while (y=stac[top--])
{
sd[y]=x;
vis[y]=0;
if (x==y) break;
p[x]+=p[y];
}
}
}
int nowCol;
vector<int> cols[maxn];
int ans[maxn];
void topo(){
queue <int> q;
for (int i=1;i<=n;i++)
if (sd[i]==i&&!in[i]){
q.push(i);
}
while (!q.empty())
{
int k=q.front();q.pop();
if(cols[k].empty()) {
ans[k] = ++nowCol;
cols[k].push_back(nowCol);
}else{
ans[k]=cols[k][0];
}
int sz=0;
for (int i=h[k];i;i=ed[i].next){
int v=ed[i].to;
if(sz<cols[k].size())cols[v].push_back(cols[k][sz++]);
in[v]--;
if (in[v]==0) q.push(v);
}
cols[k].clear();
}
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
for (int i=1;i<=n;i++)
if (!dfn[i]) tarjan(i);
for (int i=1;i<=m;i++)
{
int x=sd[edge[i].from],y=sd[edge[i].to];
if (x!=y)
{
ed[++s].next=h[x];
ed[s].to=y;
ed[s].from=x;
h[x]=s;
in[y]++;
}
}
topo();
if(nowCol>n)while(1);
for(int i=1;i<=n;i++){
printf("%d ",ans[sd[i]]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3888kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
1 1 1 2 1
result:
ok AC
Test #2:
score: 0
Accepted
time: 2ms
memory: 5696kb
input:
5 7 1 2 2 1 4 3 5 1 5 4 4 1 4 5
output:
1 1 2 1 1
result:
ok AC
Test #3:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
8 6 6 1 3 4 3 6 2 3 4 1 6 4
output:
1 1 1 1 2 1 3 4
result:
ok AC
Test #4:
score: -100
Wrong Answer
time: 3ms
memory: 6036kb
input:
7000 6999 4365 4296 2980 3141 6820 4995 4781 24 2416 5844 2940 2675 3293 2163 3853 5356 262 6706 1985 1497 5241 3803 353 1624 5838 4708 5452 3019 2029 6161 3849 4219 1095 1453 4268 4567 1184 1857 2911 3977 1662 2751 6353 6496 2002 6628 1407 4623 425 1331 4445 4277 1259 3165 4994 1044 2756 5788 5496 ...
output:
1 1948 2586 2138 368 2 3 2511 3044 2240 409 1378 4 1594 2212 5 2776 6 3140 1790 2739 2910 1616 2254 1938 7 8 755 2429 9 2920 2863 10 11 2527 2526 2330 12 1640 13 2202 2955 14 2806 15 3112 16 691 17 18 3488 19 2662 1396 1974 2112 901 20 3205 21 2882 1187 267 546 2054 22 1694 1197 2234 23 24 3158 25 3...
result:
wrong answer Integer 1948 violates the range [1, 1750]