zoukankan      html  css  js  c++  java
  • 洛谷 P1640 【连续攻击游戏】


    Solution : At first you may have no idea of this subject,but there is a very ingenious train of thought. One thought would be to add two attributes of the same equipment,but this would be too hard to solve. So we have other thought : we can add the level of the attributes of the same equipment to the number of the equitment,then we use the bipartite graph. Because in the subject we can only use one attribute of one equipment && if we add  attributes level of the same equipment to the number of the equitment,the graph of the sample will be shown as below:

    then we will realize that we can enumerate 1 ~ 10001(the attribute level) to run bipartite graph,if we can't find the equipment of that attribute level(there is no equipment of that level || that equipment used other attribute)then the answer is that attribute level - 1

    code:

     1 #include <bits/stdc++.h>
     2 #define INF 0x3f3f3f3f
     3 #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
     4 using namespace std;
     5 char buf[1 << 21];
     6 char *p1 = buf;
     7 char *p2 = buf;
     8 template < class T >
     9 inline void read(T & x)
    10 {
    11     x = 0;
    12     char c = getchar();
    13     bool f = 0;
    14     for(; !isdigit(c); c = getchar()) 
    15     {
    16         f ^= c == '-';
    17     }
    18     for(; isdigit(c); c = getchar())
    19     {
    20         x = x * 10 + (c ^ 48);
    21     }
    22     x = f ? -x : x;
    23     return;
    24 }
    25 template < class T >
    26 inline void write(T x)
    27 {
    28     if(x < 0)
    29     {
    30         putchar('-');
    31         x = -x;
    32     }
    33     T y = 1;
    34     int len = 1;
    35     for(; y <= x / 10; y *= 10)
    36     {
    37         ++len;
    38     }
    39     for(; len; --len, x %= y, y /= 10)
    40     {
    41         putchar(x / y + 48);
    42     }
    43     return;
    44 }
    45 int n, choose[1000001], vis[1000001], num, head[1000001], cnt;
    46 struct node
    47 {
    48     int next, to;
    49 }stu[2000001];
    50 inline void add(int x, int y)
    51 {
    52     stu[++num].next = head[x];
    53     stu[num].to = y;
    54     head[x] = num;
    55     return;
    56 }
    57 inline int dfs(int u)//bipartite graph masterplate 
    58 {
    59     for(register int i = head[u]; i; i = stu[i].next)
    60     {
    61         int k = stu[i].to;
    62         if(vis[k] == cnt)//it is just the same as if(vis[k]),but we can't use memset(TLE) so we can only use this
    63         {
    64             continue;
    65         }
    66         vis[k] = cnt;//(self-understanding) 
    67         if(!choose[k] || dfs(choose[k]))
    68         {
    69             choose[k] = u;
    70             return 1;
    71         }
    72     }
    73     return 0;
    74 }
    75 signed main()
    76 {
    77     read(n);
    78     for(register int i = 1, x, y; i <= n; ++i)
    79     {
    80         read(x);
    81         read(y);
    82         add(x, i);
    83         add(y, i);
    84     }
    85     for(register int i = 1; i <= 10001; ++i)//warning: to 10001 not to 10000(self-understanding) 
    86     {
    87         //warning:you can't use memset here because that will TLE(O(10000 * n))
    88         ++cnt;//leave out the memset(self-understanding) 
    89         if(!dfs(i))
    90         {
    91             write(i - 1);
    92             return 0;
    93         }
    94     }
    95     return 0;
    96 }

    //2 hrs

  • 相关阅读:
    【原创】Kakfa cluster包源代码分析
    【原创】Kakfa log包源代码分析(二)
    【原创】Kakfa log包源代码分析(一)
    【原创】Kakfa metrics包源代码分析
    【原创】Kakfa network包源代码分析
    【原创】Kakfa common包源代码分析
    【原创】Kakfa serializer包源代码分析
    基于OpenCV/TensorFlow的手写MNIST文字匹配
    随机游走 点云匹配
    OpenCV Mat&Operations
  • 原文地址:https://www.cnblogs.com/qqq1112/p/11341427.html
Copyright © 2011-2022 走看看