ZOJ 3795:grouping (indent + longest)

Source: Internet
Author: User

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5303

Test instructions: There are n person M edge, each side has a u,v, the age of the U is greater than or equal to V, now to divide the n person into x groups, the age of the group can not be directly or indirectly compared, ask at least how many groups can be divided.

Idea: At first did not see test instructions, the direct topological sort did. Later listen to brother said there will be a ring, age is equal to, if there is a ring representing the age of the same, then the ring inside the person is a group, the point after the end of the length of the point is the number of points, and then use DP the longest way to do.

1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <string>6#include <cmath>7#include <queue>8#include <vector>9#include <map>Ten#include <Set> One#include <stack> A using namespacestd; - #defineINF 0x3f3f3f3f - #defineN 100010 the structEdge { -     intV, NXT; -} edge[n*3], edg[n*3]; - intCnt[n], dp[n], head[n], hea[n], Tott, tot, Tim, Vis[n], deg[n], dfn[n], low[n], belong[n], num; +stack<int>STA; -  + voidADD (intUintv) { AEDGE[TOT].V = v; EDGE[TOT].NXT = Head[u]; Head[u] = tot++; at } -  - voidAddintUintv) { -EDG[TOTT].V = v; EDG[TOTT].NXT = Hea[u]; Hea[u] = tott++; - } -  in voidTarjan (intu) { -Dfn[u] = Low[u] = + +Tim; toSta.push (U); Vis[u] =1; +      for(inti = Head[u]; ~i; i =edge[i].nxt) { -Edge &e =Edge[i]; the         if(!DFN[E.V]) { * Tarjan (E.V); $             if(LOW[E.V] < Low[u]) Low[u] =LOW[E.V];Panax Notoginseng}Else if(VIS[E.V] && dfn[e.v] < Low[u]) Low[u] =DFN[E.V]; -     } the     if(Low[u] = =Dfn[u]) { +++num;intv =-1; ACnt[num] =0; the          while(V! =u) { +v =sta.top (); Sta.pop (); -BELONG[V] =num; $cnt[num]++; $VIS[V] =0; -         } -     } the } - Wuyi intDasointu) { the     if(Dp[u])returnDp[u]; -     intAns =Cnt[u]; Wu      for(inti = Hea[u]; ~i; i =edg[i].nxt) { -         intv =edg[i].v; Aboutans = max (ans, DFS (v) +Cnt[u]); $     } -     returnDp[u] =ans; - } -  A intMain () { +     intN, M; the      while(~SCANF ("%d%d", &n, &m)) { -Memset (Head,-1,sizeof(head)); $memset (Hea,-1,sizeof(Hea)); thememset (DFN,0,sizeof(DFN)); theMemset (DP,0,sizeof(DP)); thetot = num = Tott = Tim =0; the         intu, v; -          for(inti =0; I < m; i++) { inscanf"%d%d", &u, &v); the Add (U, v); the         } About          for(inti =1; I <= N; i++) the             if(!Dfn[i]) Tarjan (i); the          for(intU =1; U <= N; u++) { the              for(inti = Head[u]; ~i; i =edge[i].nxt) { +                 intv =edge[i].v; -                 if(Belong[u]! =Belong[v]) { the Add (Belong[u], belong[v]);Bayi                 } the             } the         } -         intAns =0; -          for(inti =1; I <= num; i++) theAns =Max (ans, DFS (i)); theprintf"%d\n", ans); the     } the     return 0; -}

ZOJ 3795:grouping (indent + longest)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.