1 solutions

  • 0
    @ 2024-12-10 23:09:38

    C :

    #include<stdio.h>
    
    int n,k,D,sum=0;
    int a[50005];
    int kind[50005];
    
    int find(int x)
    {
    	if(x==a[x]) return x;
    	int t=a[x];
    	a[x]=find(t);
    	kind[x]=(kind[x]+kind[t])%3;
    	return a[x];
    }
    
    void zuhe(int x,int y,int d)
    {
    	int r1=find(x);
    	int r2=find(y);
    	a[r1]=r2;//合并两个节点 
    	kind[r1]=(3-kind[x]+d+kind[y])%3;
    }
    
    int main()
    {
    	scanf("%d%d",&n,&k);
    	
    	for(int i=0;i<=n;++i)
    	{
    		a[i]=i;
    		kind[i]=0;
    	}
    	
    	for(int i=0;i<k;++i)
    	{
    		int x,y;
    		scanf("%d%d%d",&D,&x,&y);
    		
    		if(x>n||y>n||(D==2&&x==y)) sum++;
    		else
    		{
    			int r1=find(x);
    			int r2=find(y);
    			
    			if(r1==r2)
    			{
    				if((kind[x]-kind[y]+3)%3!=D-1)
    				{
    					sum++;
    				}
    			}
    			else
    			{
    				zuhe(x,y,D-1);
    			}
    		}
    	}
    	printf("%d",sum);
    	return 0;
    }
    
    

    C++ :

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    int relation[50010],f[50010];
    int n,k,sum=0;
    void init();
    void merge(int,int,int,int,int);
    int find(int);
    void work();
    int main()
    {
    	init();
    	work();
    	return 0;
    }
    void init()
    {
    	memset(relation,0,sizeof(relation));
    	cin>>n>>k;
    	for(int i=1;i<=n;i++) f[i]=i;	
    }
    void work()
    {
    	int d,x,y;
    	for(int i=1;i<=k;i++)
    	{
    		cin>>d>>x>>y;
    		if(x>n||y>n)
    		{
    			sum++;
    			continue;
    		}
    		if(n==2 && x==y)
    		{
    			sum++;
    			continue;
    		}
    		int fx,fy;
    		fx=find(x);
    		fy=find(y);
    		if(fx!=fy) merge(fx,fy,x,y,d);
    		else
    		if((relation[y]+3-relation[x])%3!=(d-1)) sum++;
    	}
    	cout<<sum<<endl;
    }
    int find(int x)
    {
    	if(f[x]==x) return x;
    	int fx;
    	fx=find(f[x]);//不能直接f[x]=find(f[x]),更新f[x]后就无法计算x和根的关系了;	
    	relation[x]=(relation[x]+relation[f[x]])%3;
    	f[x]=fx;//路径压缩
    	return f[x]; 
    }
    void merge(int fx,int fy,int x,int y,int d)
    {
    	f[fy]=fx;
    	relation[fy]=(3-relation[y]+d-1+relation[x])%3;
    }
    

    Pascal :

    var
      ship,father:array[1..50002] of longint;
      fb,fc,sb,sc,b,c,a,i,n,k,sum:longint;
    procedure init;
    var
      i:longint;
    begin
      readln(n,k);
      for i:=1 to n do
        begin
          ship[i]:=0;
          father[i]:=i;
        end;
      sum:=0;
    end;
    function getancestor(x:longint):longint;
    var
      temp:longint;
    begin
      temp:=x;
      while (father[temp]<>temp) do temp:=father[temp];
      getancestor:=temp;
    end;
    function getship(x:longint):longint;
    begin
      if father[x]=x then
        begin
          getship:=0;
          exit;
        end;
        ship[x]:=(ship[x]+getship(father[x])) mod 3;
        father[x]:=getancestor(x);
        getship:=ship[x];
    end;
    procedure work;
    var
      i:longint;
    begin
      for i:=1 to k do
        begin
          readln(a,b,c);
    
          if (b>n) or (c>n) or ((a=2) and (b=c)) then
            begin
              inc(sum);
              continue;
            end;
          fb:=getancestor(b);
          fc:=getancestor(c);
          sc:=getship(c);
          sb:=getship(b);
          if a=1 then
            begin
              if fc<>fb then
                begin
                  father[fc]:=b;
                  ship[fc]:=(3-sc) mod 3;
                end
                else if sb<>sc then inc(sum);
            end
            else
            begin
              if fc<>fb then
                begin
                  father[fc]:=b;
                  ship[fc]:=(2-sc) mod 3;
                end
                else if sb<>(sc+1) mod 3 then inc(sum);
            end;
        end;
    end;
    begin
      init;
      work;
      writeln(sum);
    end.
    
    
    • 1

    Information

    ID
    584
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By