Submission #959492

#TimeUsernameProblemLanguageResultExecution timeMemory
959492sleepntsheep철인 이종 경기 (APIO18_duathlon)C11
100 / 100
55 ms20820 KiB
#include<stdio.h>
#include<stdlib.h>

#define N 200005

int mini(int i,int j)
{
    return i<j?i:j;
}

typedef struct
{
    int v,nxt;
} edge;

typedef struct
{
    edge*e;
    int eo;
    int*h;
} graph;

void ginit(graph*g,int n)
{
    g->h=(int*)malloc(sizeof(g->h[0])*n);
    g->eo=0;
    g->e=(edge*)malloc(2*sizeof g->e[0]);
}

void glink(graph*g,int u,int v)
{
    int o=++g->eo;
    if(!(o&o-1))g->e=(edge*)realloc(g->e,sizeof(g->e[0])*2*o);
    g->e[o].v=v;
    g->e[o].nxt=g->h[u];
    g->h[u]=o;
}

void gbilink(graph*g,int u,int v)
{
    glink(g,u,v);
    glink(g,v,u);
}

void gclean(graph*g)
{
    free(g->e);
    free(g->h);
}

#define giter(g,u,v) for(int j=(g).h[(u)],v=(g).e[j].v;j;j=(g).e[j].nxt,v=(g).e[j].v)

int n,m;
graph g,bct;

int dfn[N],low[N],timer,id,deg[N];

int V;
static int ss[N],so=0;
void tarjan(int u,int p)
{
    ++V;

    dfn[u]=low[u]=++timer;

    ss[so++]=u;

    giter(g,u,v)
    {
        if(v==p)continue;
        if(dfn[v])
        {
            low[u]=mini(low[u],dfn[v]);
        }
        else
        {
            tarjan(v,u);

            if(low[v]>=dfn[u])
            {
                int square=id++,x;
                do gbilink(&bct,square,x=ss[--so]), ++deg[square]; while (x != v);
                gbilink(&bct,square,u),++deg[square];
            }

            low[u]=mini(low[u],low[v]);
        }
    }
}

int square(int u)
{
    return u >= n;
}

int round(int u)
{
    return !square(u);
}

int weight(int u)
{
    return (square(u) ? deg[u] : -1);
}

int sz[N];

long long dfs(graph*bct,int u,int p)
{
    long long z=0, npath=0;
    sz[u] = round(u);

    giter(*bct,u,v)
    {
        if(v==p)continue;
        z += dfs(bct,v,u);
        npath+=sz[u]*1ll*sz[v];
        sz[u]+=sz[v];
    }

    npath += sz[u]*1ll*(V-sz[u]);

    return z + npath * weight(u);
}

int main()
{
    scanf("%d%d",&n,&m);
    ginit(&g,n);
    for(int i=0,u,v;i<m;++i) scanf("%d%d",&u,&v),gbilink(&g,u-1,v-1);
    ginit(&bct,2*n);

    id=n;
    long long ans=0;
    for(int i=0;i<n;++i)
    {
        if(!dfn[i])
        {
            so=0;
            V=0;
            tarjan(i, i);
            ans += dfs(&bct,i,-1);
            bct.eo=0;
        }
    }

    printf("%lld",ans*2);

    gclean(&g);
    gclean(&bct);
}

Compilation message (stderr)

count_triplets.c: In function 'glink':
count_triplets.c:33:13: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   33 |     if(!(o&o-1))g->e=(edge*)realloc(g->e,sizeof(g->e[0])*2*o);
      |            ~^~
count_triplets.c: At top level:
count_triplets.c:96:5: warning: conflicting types for built-in function 'round'; expected 'double(double)' [-Wbuiltin-declaration-mismatch]
   96 | int round(int u)
      |     ^~~~~
count_triplets.c:3:1: note: 'round' is declared in header '<math.h>'
    2 | #include<stdlib.h>
  +++ |+#include <math.h>
    3 | 
count_triplets.c: In function 'main':
count_triplets.c:128:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |     scanf("%d%d",&n,&m);
      |     ^~~~~~~~~~~~~~~~~~~
count_triplets.c:130:30: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |     for(int i=0,u,v;i<m;++i) scanf("%d%d",&u,&v),gbilink(&g,u-1,v-1);
      |                              ^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...