zoukankan      html  css  js  c++  java
  • 紫桥杯

    C. Online Courses In BSU
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.

    The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).

    Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.

    Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.

    Input

    The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.

    The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.

    Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti(0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.

    It is guaranteed that the sum of all values ti doesn't exceed 105.

    Output

    Print -1, if there is no the way to get a specialty.

    Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.

    Examples
    input
    Copy
    6 2
    5 3
    0
    0
    0
    2 2 1
    1 4
    1 5
    output
    Copy
    5
    1 2 3 4 5
    input
    Copy
    9 3
    3 9 5
    0
    0
    3 9 4 5
    0
    0
    1 8
    1 6
    1 2
    2 1 2
    output
    Copy
    6
    1 2 9 4 5 3
    input
    Copy
    3 3
    1 2 3
    1 2
    1 3
    1 1
    output
    Copy
    -1
    Note

    In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.

    #include <iostream>
    #include<bits/stdc++.h>
    using namespace std;
    int a[100005],c[100005];
    vector<int>ans,str[100005];
    int flag=0;
    void dfs(int v)
    {
        if(c[v]==2)
            return ;
        c[v]=1;
        for(int i=0;i<str[v].size();i++)
        {
            if(c[str[v][i]]==1)
            {
                cout<<-1<<endl;
               exit(0);
            }
            dfs(str[v][i]);
        }
       ans.push_back(v);
       c[v]=2;
    }
    int main()
    {
    int n,m;
    cin>>n>>m;
    int i;
    for(i=1;i<=m;i++)
        cin>>a[i];
    int t,w,j;
    for(i=1;i<=n;i++)
    {
        cin>>t;
        for(j=1;j<=t;j++)
        {
           cin>>w;
           str[i].push_back(w);
        }
    }
    for(i=1;i<=m;i++)
        dfs(a[i]);
        cout<<ans.size()<<endl;
        for(i=0;i<ans.size();i++)
        {
            cout<<ans[i]<<" ";
        }
        return 0;
    }
    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

    Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

    You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
     
    Input
    First line contains two integers stand for N and M.

    Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

    Process to the end of the file.
     
    Output
    For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
     
    Sample Input
    7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
     
    Sample Output
    13
    #include <iostream>
    #include<bits/stdc++.h>
    using namespace std;
    struct node
    {
        int x;
        int y;
        int step;
        bool operator <(const node &t)const
        {
            return step>t.step;
        }
    };
    char mapp[202][202];
    int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
    int n,m,count;
    int sx,sy;
    void bfs()
    {
        node temp,s,now;
        priority_queue<node>que;
        s.x=sx;
        s.y=sy;
        s.step=0;
        mapp[sx][sy]='#';
       que.push(s);
       while(!que.empty())
       {
           temp=que.top();
           que.pop();
           for(int i=0;i<4;i++)
           {
                now.x=temp.x+dir[i][0];
                now.y=temp.y+dir[i][1];
            now.step=temp.step+1;
               if(now.x<0||now.x>=n||now.y<0||now.y>=m||mapp[now.x][now.y]=='#')
                continue;
               else if(mapp[now.x][now.y]=='x')
               {
                   now.step++;
               }
               else if(mapp[now.x][now.y]=='r')
               {
                    cout<<now.step<<endl;
                   return ;
               }
               mapp[now.x][now.y]='#';
               que.push(now);
           }
    
       }
       printf("Poor ANGEL has to stay in the prison all his life.
    ");
       return ;
    }
    int main()
    {
            int i,j;
            while(cin>>n>>m)
            {
           memset(mapp,' ',sizeof(mapp));
           for(i=0;i<n;i++)
           {
               scanf("%s",mapp[i]);
           }
           for(i=0;i<n;i++)
           {
               for(j=0;j<m;j++)
               {
                   if(mapp[i][j]=='a')
                   {
                       sx=i;
                       sy=j;
                       break;
                   }
               }
           }
           bfs();
            }
       return 0;
    }
    L2-024 部落 (25 分)

    在一个社区里,每个人都有自己的小圈子,还可能同时属于很多不同的朋友圈。我们认为朋友的朋友都算在一个部落里,于是要请你统计一下,在一个给定社区中,到底有多少个互不相交的部落?并且检查任意两个人是否属于同一个部落。

    输入格式:

    输入在第一行给出一个正整数N(104​​),是已知小圈子的个数。随后N行,每行按下列格式给出一个小圈子里的人:

    P[1P[2⋯ P[K]

    其中K是小圈子里的人数,P[i](i=1,,K)是小圈子里每个人的编号。这里所有人的编号从1开始连续编号,最大编号不会超过104​​。

    之后一行给出一个非负整数Q(104​​),是查询次数。随后Q行,每行给出一对被查询的人的编号。

    输出格式:

    首先在一行中输出这个社区的总人数、以及互不相交的部落的个数。随后对每一次查询,如果他们属于同一个部落,则在一行中输出Y,否则输出N

    输入样例:

    4
    3 10 1 2
    2 3 4
    4 1 5 7 8
    3 9 6 4
    2
    10 5
    3 7
    

    输出样例:

    10 2
    Y
    N
    并查集
    #include <iostream>
    #include<bits/stdc++.h>
    using namespace std;
    int s[10005],f[10005];
    int find(int x)
    {
        return s[x]==x?x:s[x]=find(s[x]);
    }
    int add(int a,int b)
    {
        int x=find(a);
        int y=find(b);
        if(x!=y)
        {
            s[x]=y;
        }
    }
    int main()
    {
      for(int i=0;i<10005;i++)
        s[i]=i;
        int n,k,a,i,m;
        cin>>n;
        while(n--)
        {
            cin>>k;
            cin>>a;
            f[a]=1;
            for(i=1;i<k;i++)
            {
                cin>>m;
                f[m]=1;
                add(a,m);
            }
        }
        int cnt=0,ans=0;
        for(i=0;i<10004;i++)
        {
            if(f[i]==1)
            {
                cnt++;
                if(s[i]==i)
                {
                    ans++;
                }
            }
    
        }
        cout<<cnt<<" "<<ans<<endl;
        int q;
        cin>>q;
        int u,v;
        while(q--)
        {
            cin>>u>>v;
            if(find(u)==find(v))
            {
                cout<<"Y"<<endl;
            }
            else
            {
                cout<<"N"<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    IOS开发之XML解析以及下拉刷新上拉加载更多的分享
    iOS之网络数据下载和JSON解析
    iOS开发常用网站
    用shell脚本打ipa包
    iOS开发之网络基础知识
    iOS开发之Block
    iOS开发之用代码实现数据库FMDB的操作
    iOS开发之下拉刷新和上拉加载
    IOS之XML解析
    IOS之网络数据下载和JSON解析
  • 原文地址:https://www.cnblogs.com/kepa/p/10492558.html
Copyright © 2011-2022 走看看