ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

关于 B - Star or Not 的一些领悟

2021-10-31 09:00:33  阅读:189  来源: 互联网

标签:10 Star cout int cin 领悟 ++ 关于 include


 


 

原题:UNICORN Programming Contest 2021(AtCoder Beginner Contest 225)

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 200200 points

Problem Statement

You are given a tree with NN vertices and N-1N−1 edges.
The vertices are numbered 1,2,\ldots,N1,2,…,N. The ii-th edge connects Vertex a_iai​ and Vertex b_ibi​.

Determine whether this tree is a star.

Here, a star is a tree where there is a vertex directly connected to all other vertices.

Notes

For the definition of a tree, see Tree (graph theory) - Wikipedia.

Constraints

  • 3 \leq N \leq 10^53≤N≤105
  • 1 \leq a_i \lt b_i \leq N1≤ai​<bi​≤N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

NN
a_1a1​ b_1b1​
\vdots⋮
a_{N-1}aN−1​ b_{N-1}bN−1​

Output

If the given graph is a star, print Yes; otherwise, print No.


Sample Input 1 Copy

5
1 4
2 4
3 4
4 5

Sample Output 1 Copy

Yes

The given graph is a star.


Sample Input 2 Copy

4
2 4
1 4
2 3

Sample Output 2 Copy

No

The given graph is not a star.


Sample Input 3 Copy

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

Sample Output 3 Copy

Yes

 

先说一下个人思路:

由于没有接触过树的学习,所以做这题时有些费劲!

个人认为,需要将两列组合起来,形成一个新的数组,

然后进行除重操作,利用最近刚学的 unique 函数 ,

实际上,利用的是 C++ 独有的 STL 容器。

再利用 sort 进行排序,如果这些数的最小值到最大值中间没有间断,

输出 “YES” ,反之,输出 “NO” 。


 

WA:

/*
      B  Star or Not
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#define maxn 1000+100
using namespace std;

int main(){
    long long n, i = 0;
    cin >> n;
    int a[maxn*2], b[maxn];
    while(n--){
        cin >> a[i] >> b[i];
        i++;
    }
    for (int j = n, c = 0; j < 2 * n, c < n; j++)
    {
        a[j] = b[c];
    }
    int len;
    len = unique(a, a + 2 * n) - a;
    for (int c = 0; c < len;c++){
        cout << a[c] <<" ";
    }
        return 0;
}

接下来,介绍下一个提速的操作:

std::ios::sync_with_stdio(false);

        经常出现程序无故超时,最终发现问题处在cin和cout上,这是因为C++中,cin和cout要与stdio同步,中间会有一个缓冲,所以导致cin,cout语句输入输出缓慢,这时就可以用这个语句,取消cin,cout与stdio的同步,说白了就是提速,效率基本与scanf和printf一致。然后就可放心的使用cin,cout了。

 

tip:

其实cin和cout也可以像scanf和printf一样快

 

只不过是C++为了兼容C,避免一些问题

 

cin构造出来就会调用cin.tie();与cout绑定

 

cin的每次格式化输入都会调用cout.flush();导致效率低

 

可以使用cin.tie(NULL);解除绑定

 

cout.tie(NULL);

 


 

AC:

 

#include <iostream>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, x, y;
    cin >> n;
    int a[n+1] = {0};
    for(int i = 1; i < n; i++){
        cin >> x >> y;
        a[x]++;
        a[y]++;
    }
    x = 0;
    for(int i = 1; i <= n; i++) if(a[i] > 1) x++;
    if(x == 1) cout << "Yes";
    else cout << "No";
    return 0;
}

 

有些小细节在里面,上面介绍的提速操作就运用到了!

还有些大佬用 map 和 vector 来写:

MAP

#include <bits/stdc++.h> using namespace std; typedef long long int ll;
int main() {     map<int, int> deg;     int n;     cin >> n;     int flag = 0;     for (int i = 0; i < n - 1; i++)     {         int a, b;         cin >> a >> b;         deg[a]++;         deg[b]++;         if (deg[a] == n - 1)             flag = 1;         if (deg[b] == n - 1)             flag = 1;     }     if (flag)         cout << "Yes" << endl;     else         cout << "No" << endl; }

VECTOR

#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> v(n + 1, 0);
    for (int i = 1; i <= n - 1; i++){
        int a, b;
        cin >> a >> b;
        v[a]++;
        v[b]++;
    }
    for (int i = 1; i <= n; i++){
        if (v[i] == n - 1){
            cout << "Yes" << "\n";
            return 0;
        }
    }
    cout << "No" << "\n";
    return 0;
}

显然 map 比 vector 更加高效!

 

标签:10,Star,cout,int,cin,领悟,++,关于,include
来源: https://www.cnblogs.com/GitHubhacker/p/15488316.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有