QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#544639#7860. Graph of Maximum Degree 3XfJbUhpyzgaW#TL 0ms0kbC++143.3kb2024-09-02 19:17:372024-09-02 19:17:38

Judging History

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

  • [2024-09-02 19:17:38]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-09-02 19:17:37]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define N 150010
#define P 998244353
#define GC getchar()
#define PC putchar
using namespace std;
ll re(){
    ll s=0,b=1; char c=GC;
    while(c>'9'||c<'0'){if(c=='-')b=-b; c=GC;}
    while(c<='9'&&c>='0'){s=(s<<1)+(s<<3)+(c^48); c=GC;}
    return s*b;
}
void ks(ll s){
    if(s<0)PC('-'),s=-s;
    if(s>=10)ks(s/10); PC((s%10)|48);
}
int n,m,b[2][N][2],s[2],ds[2][N],vi[N],dl[N],h,t,ytt,yhe[N],x,y,op;
int bh[N],nn,he[N],tt,li[N],cd;
bool V[N];
ll ans;
struct A{int ne,to,co;}yb[N<<3];
struct B{int ne,to;}B[N<<2];
map<int,int>bt[N];
void ad(int x,int y){
    B[++tt].to=y; B[tt].ne=he[x]; he[x]=tt;
    B[++tt].to=x; B[tt].ne=he[y]; he[y]=tt;
}
void dfs(int x,int f,int zz){
    li[++cd]=x; V[x]=1;
    for(int i=he[x]; i; i=B[i].ne){
        int v=B[i].to; if(v==f)continue;
        if(v==zz)op=1; else dfs(v,x,zz);
    }
}
void pd2(int x,int y){
    if(bt[x][y])ans++;
}
void pd3(int x,int y,int z){
    int s=bt[x][y]+bt[y][z]+bt[z][x];
    if(s>=2)ans++;
}
void pd4(int x,int y,int z,int a){
    int s=bt[x][y]+bt[y][z]+bt[x][z]+bt[x][a]+bt[y][a]+bt[z][a];
    //cout<<s<<endl;
    if(s>3){ans++;}
    if(s==3){
        if(bt[x][y]||bt[x][z]||bt[x][a])
         if(bt[y][x]||bt[y][z]||bt[y][a])
          if(bt[z][x]||bt[z][y]||bt[z][a])
           if(bt[a][x]||bt[a][y]||bt[a][z])ans++;
    }
}
int main(){
    freopen("a.in","r",stdin);
    n=re(),m=re(); ans=n;
    for(int i=1; i<=m; ++i){
        x=re(),y=re(),op=re();
        s[op]++;
        b[op][s[op]][0]=x;
        b[op][s[op]][1]=y;
        ds[op][x]++; ds[op][y]++;
        yb[++ytt].to=y; yb[ytt].ne=yhe[x]; yhe[x]=ytt; yb[ytt].co=op;
        yb[++ytt].to=x; yb[ytt].ne=yhe[y]; yhe[y]=ytt; yb[ytt].co=op;
    } h=1;
    for(int i=1; i<=n; ++i)
     if(ds[0][i]==0||ds[1][i]==0)dl[++t]=i,vi[i]=1;
    while(h<=t){
        x=dl[h++];
        for(int i=yhe[x]; i; i=yb[i].ne){
            int v=yb[i].to,co=yb[i].co; ds[co][v]--;
            if(ds[co][v]==0){dl[++t]=v; vi[v]=1;}
        }
    }
    for(int i=1; i<=n; ++i) if(!vi[i])bh[i]=++nn; //cout<<nn<<endl;
    for(int i=1; i<=s[0]; ++i){
        if(bh[b[0][i][0]]&&bh[b[0][i][1]])
         ad(bh[b[0][i][0]],bh[b[0][i][1]]);
    }
    for(int i=1; i<=s[1]; ++i){
        if(bh[b[1][i][0]]&&bh[b[1][i][1]])
         bt[bh[b[1][i][0]]][bh[b[1][i][1]]]=bt[bh[b[1][i][1]]][bh[b[1][i][0]]]=1;
    }
    for(int i=1; i<=nn; ++i){
        if(!V[i]){
            op=cd=0; dfs(i,-1,i);// cout<<cd<<' ';
            if(cd==2)pd2(li[1],li[2]);
            else for(int j=1; j<=cd; ++j){
                if(j<cd){
                    pd2(li[j],li[j+1]);
                } else{
                    if(op)pd2(li[j],li[1]);
                }
            }
            if(cd==3)pd3(li[1],li[2],li[3]);
            else{
                for(int j=1; j<=cd; ++j){
                    if(j<cd-1)pd3(li[j],li[j+1],li[j+2]);
                     else if(op)pd3(li[j],li[j%cd+1],li[(j+1)%cd+1]);
                }
            }
            if(cd==4)pd4(li[1],li[2],li[3],li[4]);
            else{
                for(int j=1; j<=cd; ++j){
                    if(j<cd-2)pd4(li[j],li[j+1],li[j+2],li[j+3]);
                     else if(op)pd4(li[j],li[j%cd+1],li[(j+1)%cd+1],li[(j+2)%cd+1]);
                }
            }
        }
    } ks(ans);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

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

output:


result: