zoukankan      html  css  js  c++  java
  • codeforces 583A Asphalting Roads

    A. Asphalting Roads

     
     

    City X consists of n vertical and n horizontal infinite roads, forming n × n intersections. Roads (both vertical and horizontal) are numbered from 1 to n, and the intersections are indicated by the numbers of the roads that form them.

    Sand roads have long been recognized out of date, so the decision was made to asphalt them. To do this, a team of workers was hired and a schedule of work was made, according to which the intersections should be asphalted.

    Road repairs are planned for n2 days. On the i-th day of the team arrives at the i-th intersection in the list and if none of the two roads that form the intersection were already asphalted they asphalt both roads. Otherwise, the team leaves the intersection, without doing anything with the roads.

    According to the schedule of road works tell in which days at least one road will be asphalted.

    Input

    The first line contains integer n (1 ≤ n ≤ 50) — the number of vertical and horizontal roads in the city.

    Next n2 lines contain the order of intersections in the schedule. The i-th of them contains two numbers hi, vi (1 ≤ hi, vi ≤ n), separated by a space, and meaning that the intersection that goes i-th in the timetable is at the intersection of the hi-th horizontal and vi-th vertical roads. It is guaranteed that all the intersections in the timetable are distinct.

    Output

    In the single line print the numbers of the days when road works will be in progress in ascending order. The days are numbered starting from 1.

    Sample test(s)
    Input
    2
    1 1
    1 2
    2 1
    2 2
    Output
    1 4 
    Input
    1
    1 1
    Output
    1 
    Note

    In the sample the brigade acts like that:

    1. On the first day the brigade comes to the intersection of the 1-st horizontal and the 1-st vertical road. As none of them has been asphalted, the workers asphalt the 1-st vertical and the 1-st horizontal road;
    2. On the second day the brigade of the workers comes to the intersection of the 1-st horizontal and the 2-nd vertical road. The 2-nd vertical road hasn't been asphalted, but as the 1-st horizontal road has been asphalted on the first day, the workers leave and do not asphalt anything;
    3. On the third day the brigade of the workers come to the intersection of the 2-nd horizontal and the 1-st vertical road. The 2-nd horizontal road hasn't been asphalted but as the 1-st vertical road has been asphalted on the first day, the workers leave and do not asphalt anything;
    4. On the fourth day the brigade come to the intersection formed by the intersection of the 2-nd horizontal and 2-nd vertical road. As none of them has been asphalted, the workers asphalt the 2-nd vertical and the 2-nd horizontal road.
     1 #include<cstdio>
     2 #include<cstring>
     3 using namespace std;
     4 
     5 bool G[55][55];
     6 int n;
     7 
     8 bool check(int h,int v)
     9 {
    10     for(int i=1;i<=n;i++)
    11         if(G[i][v])
    12             return false;
    13     for(int i=1;i<=n;i++)
    14         if(G[h][i])
    15             return false;
    16     return true;
    17 }
    18 
    19 int main()
    20 {
    21     while(scanf("%d",&n)!=EOF)
    22     {
    23         memset(G,0,sizeof(G));
    24         bool key1=0;
    25         for(int k=1;k<=n*n;k++)
    26         {
    27             int h,v;
    28             scanf("%d%d",&h,&v);
    29             bool key2=check(h,v);
    30             if(key2&&!key1)
    31             {
    32                 key1=1;
    33                 printf("%d",k);
    34                 G[h][v]=1;
    35             }
    36             else if(key2&&key1)
    37             {
    38                 printf(" %d",k);
    39                 G[h][v]=1;
    40             }
    41         }
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    C#面向过程之类型转换、算术运算符、关系运算符、逻辑运算符、if-else语句、switch-case、循环结构(while、for)、三元表达式
    C#面向过程之编译原理、变量、运算符
    VS快捷键整理
    简单聊聊mybatis插件(附源码)
    高性能页面加载技术(流水线加载)BigPipe的C#简单实现(附源码)
    聊聊js运算符 ‘与(&&)’和‘ 或(||)’
    从内部剖析C# 集合之--Dictionary
    从内部剖析C# 集合之---- HashTable
    字符串查找和函数操作题目解析
    常用排序算法实现
  • 原文地址:https://www.cnblogs.com/homura/p/4991482.html
Copyright © 2011-2022 走看看