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

  • 相关阅读:
    第27篇-虚拟机字节码指令之操作数栈管理指令
    第26篇-虚拟机对象操作指令之putstatic
    第25篇-虚拟机对象操作指令之getfield
    第24篇-虚拟机对象操作指令之getstatic
    第23篇-虚拟机字节码指令之类型转换
    第22篇-虚拟机字节码之运算指令
    第20篇-加载与存储指令之ldc与_fast_aldc指令(2)
    第19篇-加载与存储指令(1)
    第18章-x86指令集之常用指令
    第17章-x86-64寄存器
  • 原文地址:https://www.cnblogs.com/qqq1112/p/11341427.html
Copyright © 2011-2022 走看看