51Node 1499 Figure

Source: Internet
Author: User
Tags abs time limit

1499 Figure
Base time limit: 2 seconds Space limit: 262144 KB score: 80 Difficulty: 5-level algorithm topic collection focus on
to a graph, you want to divide these points into a, b two sets, so that the export sub-graph satisfying A is a complete graph, and B's export sub-graph is a non-edge Figure.
But in fact you don't have to be able to do it, so you allow mistakes. We define a perfect value of:
1. If the two points in a are connected, the perfect value of |i-j| is increased.
2. If two points in B are unbounded, increase the perfect value of the |i-j|.
(I,j is the number of these two points)
so we can make the perfect value the best.
N <=, M <= 200000
Input
N, m for points and sides
m rows, and
U,v for an no-forward edge.
(there will be no heavy and self-loops)
Output
A number that represents the maximum perfect value.
Input Example
5 5
1 2
1 3
1 4
1 5
2 3
Output example
one

Normal solution #include <bits/stdc++.h> #define FIN freopen ("Input.txt", "R", stdin);
#define FOUT freopen ("Output.txt", "w+", stdout);
using namespace Std;

typedef long Long LL;
const int INF = 0X3F3F3F3F;
const int mod = 998244353;
const double eps=1e-8;
Const double Pi=acos (-1.0);

const int N = 400010; The int map[1005][1005];//adjacency matrix, which holds the point-to-struct node {int x,y,id of the connected Edge;}
Node[n];
struct Edge {int to,cap,rev;//end point, capacity, reverse side};
The adjacency matrix of the vector<edge>g[n];//graph represents the distance designator int iter[n];//the access token int level[n];//vertex to the source point used in the bool Used[n];//dfs, the current arc, which is useless on the previous side.
    BOOL CMP (Node A,node b) {if (a.x==b.x) return a.y>b.y;
else return a.x<b.x;
    }//To add an edge void Addedge (int u,int v,int W) {G[u].push_back (edge) {v,w,g[v].size ()}) from S to t capacity to cap for the graph; G[v].push_back (Edge) {u,w,g[u].size ()-1});//If it is a non-direction graph, just change the 0 to W can}//through the BFS calculate the distance from the source point label void BFs (int s) {memset (level,-1,
    sizeof (level));
    Queue<int> que;
    level[s]=0;
    Que.push (s); while (!que.empty ()) {int V=que. Front ();
        Que.pop ();
            for (int i=0;i<g[v].size (); i++) {Edge &e=G[v][i];
                if (e.cap>0&&level[e.to]<0) {level[e.to]=level[v]+1;
            Que.push (e.to);
    }}}//Through DFS find augmented path int dfs (int v,int t,int f) {if (v==t) return F;
        for (int &i=iter[v];i<g[v].size (); i++) {Edge &e=G[v][i];
            if (e.cap>0&&level[v]<level[e.to]) {int D=dfs (e.to,t,min (F,e.cap));
                if (d>0) {e.cap-=d;
                G[e.to][e.rev].cap+=d;
            return D;
}}} return 0;
    }//solves the maximum stream int max_flow (int s,int t) from S to t {int flow=0;
    for (;;)
        {BFS (s);
        if (level[t]<0) return flow;
        memset (Iter,0,sizeof (ITER));
        int F;
    while ((F=dfs (S,t,inf)) >0) flow+=f;
}} void Init (){memset (map,0,sizeof (MAP));
for (int i=0;i<n;i++) g[i].clear ();
    } int main () {int n,m;
        while (~SCANF ("%d%d", &n,&m)) {Init ();
        int s=0,t=n+1;
        int u,v;
            for (int i=0; i<m; i++) {scanf ("%d%d", &u,&v);
            Map[u][v]=1;
        Map[v][u]=1;
        } int sum=0; for (int i=1, i<=n; i++) for (int j=1; j<=n; j + +) Sum+=abs (i-j);//Find out the value of all point pairs//prin
        TF ("sum:%d\n", sum); for (int i=1, i<=n; i++) {for (int j=i+1; j<=n; J + +) {Addedge (i,j,ab S (i-j));//Any point pair between a |u-v| edge if (map[i][j]==1)//For each edge (u,v), a U-, t capacity of |u-v|, even a V-t capacity of |u-v|
                The Edge.
                    {//printf ("<--%d%d-->\n", i,j);
                    Addedge (I,t,abs (i-j));
                Addedge (J,t,abs (i-j)); } else//no edge for each point pair (u,v), with an S-u capacity of |u-v|, and an edge of S-v capacity of |u-v|.
                    {//printf ("!--%d%d--! \ n", i,j);
                    Addedge (S,i,abs (i-j));
                Addedge (S,j,abs (i-j));
        }}} int Mincut=max_flow (s,t);//For Minimum cut//printf ("mincut:%d\n", mincut);
 printf ("%d\n", (sum-mincut)/2);//Final result}}

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.