zoukankan      html  css  js  c++  java
  • 2020.10.03天梯赛补题报告

    7-9 排座位

    https://pintia.cn/problem-sets/1311653744057376768/problems/1311836431267258368

    思路:数据较小,可直接用邻接矩阵来存储图结构。如果两人使朋友,就输出NO problem, 不是朋友但也不是敌人,则输出OK, 是敌人但有共同朋友,则输出OK but..., 只有敌人关系,则输出no way;

    下附代码:

      

    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    typedef long long ll;
    const int N = 10010;
    int g[110][110];
    int main()
    {		
    	int n, m, k;
    	cin >> n >> m >> k;
    	for(int i = 0;i < m; ++ i){
    		
    		int x, y, state;
    		cin >> x >> y >> state;
    		g[x][y] = state;
    		g[y][x] = state;
    	
    	}
    	for(int i = 0;i < k ; ++ i){
    	
    		int x, y, flag = 0;
    		cin >> x >> y;
    		
    		if(g[x][y] == -1){
    			
    			for(int j = 1;j <= n; ++ j){
    				if(g[x][j]  == 1&& g[x][j] == 1){
    					flag = 1;
    					cout << "OK but...
    ";
    					break;
    				}
    			}
    			
    			if(!flag)
    			cout << "No way
    ";
    		}
    		
    		if(g[x][y] == 1){
    			cout << "No problem
    ";
    		}
    		
    		if(g[x][y] == 0) {
                 cout << "OK
    ";
    		}
    		
    	}
    }
    

    7-11 重排链表 (25分)

    https://pintia.cn/problem-sets/1311653744057376768/problems/1311836431267258370  

    思路:

    用一个结构体存储链表节点信息,包括地址,数据,指向下一个的地址,然后用一个数组将连边从头节点到尾节点串起来,最后依照题目输出...

    #include <bits/stdc++.h>
    
    using namespace std;
    const int N = 100010;
    
    struct node{
     int pos, data, next;
    }a[N];
    
    vector<node> k;
    int main()
    {
    	int s, n;
    	scanf("%d %d", &s, &n);
    	
    	for(int i = 1;i <= n; ++ i){
    		int x, y, z;
    		scanf("%d %d %d", &x, &y, &z);
    		a[x].pos = x;
    		a[x].data = y;
    		a[x].next = z;
    	}
    	
    	while(1){
    		k.push_back(a[s]);
    		if(a[s].next == -1) break;
    		s = a[s].next;
    	}
    	
    //	for(int i = 0;i < k.size(); ++ i){
    //		printf("%05d ", k[i].pos);
    //	}
    
    	int q1 = 0, q2 = k.size() - 1;
    // 	puts("");
    	while(q1 <= q2){
    		if(q1 == q2){
    			printf("%05d %d -1", k[q1].pos, k[q1].data, k[q2].pos);
    			break;
    		}
    		
     		printf("%05d %d %05d
    ", k[q2].pos, k[q2].data, k[q1].pos);
    		
    		if(q1 == q2 - 1)
     		printf("%05d %d -1", k[q1].pos, k[q1].data, k[q2].pos);
     		else
     		printf("%05d %d %05d
    ", k[q1].pos, k[q1].data, k[q2-1].pos);
    	 	q1 ++;
    		q2 --;	
    	}
    }
    

     7-12 分而治之 (25分)

    https://pintia.cn/problem-sets/1311653744057376768/problems/1311836431267258371

    题目大意:

    给出n个点和m条边,问拿掉几个点这个图还有无连通的地方,若该图任意两点之间都无边,则输出YES, 否则输出NO。

    思路:

    用两个数组存储连通的边,用另一个数组表示该节点是否存在;当删除该节点时,该节点消失;经过一番操作,删掉几个节点,然后再用前两个数组判断还有无连通的边...

    #include <bits/stdc++.h>
    
    using namespace std;
    const int N = 10010;
    
    int a[N], b[N], c[N], v, n;
    
    int main()
    {
        cin >> v >> n;
        for(int i = 1;i <= n; ++ i){
            cin >> a[i] >> b[i];
        }
        
        int q, k;
        cin >> q;
        while(q --){
            memset(c, 0, sizeof(c) );
            cin >> k;
            for(int i = 0;i < k; ++ i){
                int x;
                cin >> x;
                c[x] = 1;
            }
            
            bool flag = true;
            
            for(int i = 1;i <= n; ++ i){
            
                if(c[a[i]] == 0 && c[b[i]] == 0){
                    flag = false;
                    break;
                }
            }
            if(flag) puts("YES");
            else puts("NO");
        }
    }
  • 相关阅读:
    作业5

    Linux系统管理4
    作业
    递归训练1:在两个长度相等的排序数组中找到上中位数
    LeetCode:面试题 08.05. 递归乘法
    LeetCode:面试题 08.06. 汉诺塔问题
    LeetCode:22. 括号生成
    如何仅用递归函数和栈操作逆序一个栈
    LeetCode:面试题 03.02. 栈的最小值
  • 原文地址:https://www.cnblogs.com/DefineWaAc/p/13777392.html
Copyright © 2011-2022 走看看