QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#105629#6402. MEXimum Spanning TreezhouhuanyiTL 1356ms5600kbC++112.3kb2023-05-14 16:32:132023-05-14 16:33:07

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-14 16:33:07]
  • 评测
  • 测评结果:TL
  • 用时:1356ms
  • 内存:5600kb
  • [2023-05-14 16:32:13]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#define N 2000
#define M 4000000
#define inf 1e9
using namespace std;
int read()
{
    char c=0;
    int sum=0;
    while (c<'0'||c>'9') c=getchar();
    while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
    return sum;
}
int n,m,d,s,t,len,rt[N+1],lg[N+1],pv[N+1],c[N+1],X[N+1],Y[N+1],Z[N+1],cnt[N+1];
bool used[N+1],vis[N+1];
vector<int>E[N+1];
void add(int x,int y)
{
    E[x].push_back(y);
    return;
}
int find(int x)
{
    if (rt[x]==x) return x;
    return rt[x]=find(rt[x]);
}
void unionn(int x,int y)
{
    rt[x]=y;
    return;
}
void add_edge()
{
    len=0;
    for (int i=s;i<=t;++i) vis[i]=pv[i]=0,E[i].clear();
    for (int i=0;i<=m;++i) cnt[i]=0;
    for (int i=1;i<=n;++i) rt[i]=i;
    for (int i=1;i<=m;++i)
	if (used[i])
	{
	    cnt[Z[i]]++;
	    if (find(X[i])!=find(Y[i])) unionn(find(X[i]),find(Y[i]));
	}
    for (int i=1;i<=m;++i)
	if (!used[i])
	{
	    cnt[Z[i]]++;
	    if (cnt[Z[i]]<=c[Z[i]]) add(s,i);
	    cnt[Z[i]]--;
	    if (find(X[i])!=find(Y[i])) add(i,t);
	}
    for (int i=1;i<=m;++i)
	if (used[i])
	{
	    for (int j=1;j<=n;++j) rt[j]=j;
	    for (int j=1;j<=m;++j)
		if (used[j]&&find(X[j])!=find(Y[j])&&j!=i)
		    unionn(find(X[j]),find(Y[j]));
	    for (int j=1;j<=m;++j)
		if (!used[j])
		{
		    cnt[Z[i]]--,cnt[Z[j]]++;
		    if (cnt[Z[j]]<=c[Z[j]]) add(i,j);
		    cnt[Z[i]]++,cnt[Z[j]]--;
		    if (find(X[j])!=find(Y[j])) add(j,i);
		}
	}
    return;
}
bool find_rd()
{
    int top;
    add_edge();
    queue<int>q;
    q.push(s),vis[s]=1;
    while (!q.empty())
    {
	top=q.front(),q.pop();
	for (int i=0;i<E[top].size();++i)
	    if (!vis[E[top][i]])
		vis[E[top][i]]=1,pv[E[top][i]]=top,q.push(E[top][i]);
    }
    return vis[t];
}
void solve()
{
    int x=pv[t];
    while (x!=s) used[x]^=1,x=pv[x];
    return;
}
bool check(int x)
{
    int res=0;
    c[x]=1,add_edge(),find_rd(),solve();
    for (int i=1;i<=m;++i) res+=used[i];
    return res==x+1;
}
int main()
{
    for (int i=2;i<=N;++i) lg[i]=lg[i>>1]+1;
    n=read(),m=read(),s=0,t=m+1;
    for (int i=1;i<=m;++i) X[i]=read(),Y[i]=read(),Z[i]=read();
    for (int i=0;i<=m;++i)
	if (!check(i))
	{
	    printf("%d\n",i);
	    return 0;
	}
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 3700kb

input:

4 4
1 2 0
2 3 1
1 3 1
3 4 2

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 1356ms
memory: 5600kb

input:

1000 1000
647 790 6
91 461 435
90 72 74
403 81 240
893 925 395
817 345 136
88 71 821
831 962 53
164 270 298
14 550 317
99 580 81
26 477 488
977 474 861
413 483 167
872 675 17
819 327 449
594 242 68
381 983 319
867 582 358
869 225 669
274 352 392
40 388 998
246 477 44
508 979 286
483 776 71
580 438 6...

output:

502

result:

ok 1 number(s): "502"

Test #3:

score: -100
Time Limit Exceeded

input:

900 1000
232 890 107
425 399 19
5 74 753
105 333 163
779 42 582
359 647 524
767 409 48
239 780 443
484 489 546
97 634 562
627 866 714
500 357 590
60 728 591
407 686 210
547 32 370
76 772 500
407 584 772
73 699 69
332 847 516
829 754 727
562 756 678
819 303 128
781 667 263
535 672 767
89 762 216
878 ...

output:


result: