zoukankan      html  css  js  c++  java
  • UVA

    //这题虽然不算难题,而且因为还没学图论,不知道无向图是什么,所以查题解磕磕碰碰地完成了,过程可谓是一波三折,不过也算收获良多


    /*
      法一:
      参考blog1: http://blog.csdn.net/shihongliang1993/article/details/73500018
      这个博主用了许多C++11的新特性,看得我大开眼界,将他用到的几种新用法弄明白,都用了不少的时间,不过也非常值得
      
      1. unordered_map 和 unordered_set
      有关博客:
      http://blog.csdn.net/liuhongxiangm/article/details/17395831
      http://blog.csdn.net/vevenlcf/article/details/51743058
      
      以及,默认情况下unordered_map只支持primitive type作为其key,若使用用户自定义的key,需要传入用户自定义的hash function,见博客:
      http://blog.csdn.net/missshirly/article/details/51231836
      
      
      特别要注意的一点是:在unordered_map的key为自定义类型时,必须重载 == 或 (),见:
      https://www.zhihu.com/question/30921173
      
      2. auto变量的使用,主要出现在:
      auto h1 = std::hash<T1>{}(p.first);
      和  for(auto &p:dic)if(p.second!=0){flag=0;break;}
      
      有关博客:
      http://blog.csdn.net/yhl_leo/article/details/50864612
      http://blog.csdn.net/huang_xw/article/details/8760403
      
      
      3. using的使用, using 和 typedef 的区别
      见:https://www.zhihu.com/question/25102205
      一句话概括:using 可以用于模板别名,typedef 不可用于模板别名
      
      
      4. 再次再次强调:
      对于map,在使用map[key]前,必须先检查一下key是否存在
      如果map不包含key,使用下标有一个危险的副作用,会在map中插入一个key的元素,value取默认值,返回value。也就是说,map[key]不可能返回null
      (这个笔记是之前做题时就写下的,然而今天居然又忘记了...)
      
      
      5.C++11中,for循环的新用法:
      
        int main()
    	{
    		std::vector<int> arr;
    		arr.push_back(1);
    		arr.push_back(2);
    
    		for (auto n : arr)
    		{
    			std::cout << n << std::endl;
    		}
    
    		return 0;
    	}
    	
    	另外,上述方式是只读,如果需要修改arr里边的值,可以使用for(auto& n:arr)
    	for循环的这种使用方式的内在实现实际上还是借助迭代器的,所以如果在循环的过程中对arr进行了增加和删除操作,那么程序将对出现意想不到的错误。
    
    	见博客: http://www.cnblogs.com/jiayayao/p/6138974.html
    	
       *****注意!!!!这个博主的代码,采用了C++11标准,如果不做一些改动,是不能在DevC下成功编译的,而要做的改动见下:
      
      贴吧地址:http://tieba.baidu.com/p/2933212687 (如果贴吧失效了,可以看下面的说明)
      a. 首先保证 gcc 版本 >= 4.8.1(只有 4.8.1 及以上的版本才能完全支持 C++11)
      b. 如果第1个条件能保证,那么就要对 DEV-C++ 设置了,具体步骤如下:
      工具 -> 编译器选项->程序(将 g++ 修改为 g++ -std=c++11 )
      
      如果是其他的编译器,报编译错时,也应该搜索下,这个编译器怎样设置,才能采用C++11的标准来编译
      例如:我当时试了各种搜索的关键词搭配,最后发现,搜索"devc++ c11标准"才得到了设置方法 T^T
      
    */
    #include <iostream>
    #include <unordered_map>
    using namespace std;
    struct pair_hash
    {
    	template <class T1, class T2>
    	size_t operator () (const pair<T1, T2> &p) const
    	{
    		auto h1 = hash<T1>{}(p.first);
    		auto h2 = hash<T2>{}(p.second);
    		return h1 ^ h2;
    	}
    };
    
    using Vote = pair<int, int>;
    using Unordered_map = unordered_map<Vote, int, pair_hash>;
    
    int main()
    {
    	int n, a, b;
    	while (cin >> n && n)
    	{
    		Unordered_map dic;
    		for (int i = 0; i < n; i++)
    		{
    			cin >> a >> b;
    			auto p = make_pair(a, b), q = make_pair(b, a);
    			if (dic.find(p) != dic.end()) dic[p]++;
    			else if (dic.find(q) != dic.end()) dic[q]--;
    			else dic[p] = 1;
    		}
    		int flag = 1;
    		for (auto &p: dic)
    		if (p.second != 0)
    		{
    			flag = 0;
    			break;
    		}
    		cout << (flag ? "YES" : "NO") << endl;
    	}
    	
    	return 0;
    }


    /*
      法二:
      这个方法采用的是邻接矩阵的思路,虽然还没学到数据结构,但是看到这个博主的代码,却也不由感慨...唉,他怎么就能想到这么简单优雅的做法呢!~连我这样还没学数据结构的人,都觉得他的方法实在是...太好了!
      
      博客:http://blog.csdn.net/monkeyduck/article/details/16335485
    */


    #include <iostream>
    #include <cstring>
    using namespace std;
    const int maxn = 1005;
    int map[maxn][maxn];
    bool is_ok()
    {
    	for (int i = 0; i < maxn; i++)
    	for (int j = 0; j < maxn; j++)
    	{
    		if (map[i][j] != 0)
    		return 0;
    	}
    	return 1;
    }
    
    int main()
    {
    	cin.tie(0);
    	cin.sync_with_stdio(false);
    	
    	int n, a, b;
    	while (cin >> n && n)
    	{
    		memset(map, 0, sizeof(map));
    		
    		for (int i = 0; i < n; i++) 
    		{
    			cin >> a >> b;
    			map[a][b]++;
    			map[b][a]--;
    		}
    		
    		if (n % 2)
    		{
    			cout << "NO" << endl;
    			continue;
    		}
    		
    		if ( is_ok() ) cout << "YES" << endl;
    		else cout << "NO" << endl;
    		
    	}
    	return 0;
    
    



    /*法三:
      这个博主的思路是:
      定义node结构体,并用它建立两个数组
      对于n个学生,每次输入a、b,将(a, b)存入第一个数组,(b, a)存入第二个数组
      
      如果恰好满足交换条件,那么这两个数组,按照同样的排序标准,经过排序后,应该是完全相同的
      
      思路借鉴自博客:
      http://blog.csdn.net/keshuai19940722/article/details/10201143
      
      法三和之前做过的一道题有些像,虽然那题是映射的思路,但两者的思路上有些相通之处,附上题号和地址,一起复习效果更佳:
      UVA - 1339 Ancient Cipher
      http://blog.csdn.net/mofushaohua_ln/article/details/77487633
    */

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int maxn = 5e5 + 100;
    
    struct node
    {
    	int x, y;
    	bool operator < (const node& a) const
    	{
    		if (x != a.x) return x < a.x;
    		if (y != a.y) return y < a.y;
    		return false;
    	}
    } tep[maxn], cur[maxn];  // temp, current
    
    int main()
    {
    	cin.tie(0);
    	cin.sync_with_stdio(false);
    	
    	int n;
    	while (cin >> n && n)
    	{
    		memset(tep, 0, sizeof(tep));
    		memset(cur, 0, sizeof(cur));
    		for (int i = 0; i < n; i++)
    		{
    			cin >> tep[i].x >> tep[i].y;
    			cur[i].x = tep[i].y;
    			cur[i].y = tep[i].x;
    		}
    		
    		sort(tep, tep + n);
    		sort(cur, cur + n);
    		
    		int flag = 1;
    		for (int i = 0; i < n; i++)
    		if (tep[i].x != cur[i].x || tep[i].y != cur[i].y)
    		{
    			flag = 0; break;
    		}
    		if (flag) cout << "YES" << endl;
    		else cout << "NO" << endl;
    	}
    	
    	return 0;
    }



  • 相关阅读:
    .NET Interop 工具集
    关于正弦波的算法
    Windows Phone 系列 本地数据存储
    Xaml cannot create an instance of “X”
    Windows Phone 系列 使用 MVVM绑定时无法获取当前值
    Windows Phone 系列 应用程序图标无法显示
    Windows Phone 系列 WPConnect无法上网的问题
    Windows Phone 系列 使用 Windows Phone 保存铃声任务
    WP7.5提交应用
    Windows Phone 系列 动态删除ObservableCollection
  • 原文地址:https://www.cnblogs.com/mofushaohua/p/7789427.html
Copyright © 2011-2022 走看看