CF320B Ping-Pong (Easy Version) 解题报告
1 题目链接
https://codeforces.com/problemset/problem/320/B
2 题目整理
题目 :乒乓球(简单版)
题目描述
在这个题目中,您可以从 ( a , b ) (a,b) (a,b)移动到 ( c , d ) (c,d) (c,d),仅当 c < a < d c < a < d c<a<d或者 c < b < d c < b < d c<b<d。
现在给你一个空集,会有两种操作:
1 a b
,表示将 ( a , b ) (a,b) (a,b)加入到此集合中。2 a b
,表示询问:第 a a a个加入的数对是否能通过一系列的移动移动到第 b b b个加入的数对。你需要对这个操作输出答案。
输入格式
第一行一个整数 n n n,表示操作的数量。
接下来 n n n行,每行有三个整数 o p t i , a i , b i opt_i,a_i,b_i opti,ai,bi,表示第 i i i次操作。
输出格式
对于每一个 o p t i = 2 opt_i = 2 opti=2的操作,你的程序需要输出。如果可以从 a i a_i ai走到 b i b_i bi,输出 " Y E S " "YES" "YES",否则输出 " N O " "NO" "NO"。
样例输入1
5
1 1 5
1 5 11
2 1 2
1 2 9
2 1 2
样例输出1
NO
YES
数据范围
对于 100 % 100\% 100%的数据:
- 1 ≤ n ≤ 100 1 \leq n \leq 100 1≤n≤100
3 题意分析
3.1 题目大意
你可以从 ( a , b ) (a,b) (a,b)移动到 ( c , d ) (c,d) (c,d),仅当 c < a < d c < a < d c<a<d或者 c < b < d c < b < d c<b<d。现在给你 n n n个操作和一个空集,分为两种:
1 a b
表示把 ( a , b ) (a,b) (a,b)加进这个集合。2 a b
表示询问第 a a a个数对是否可以移动到第 b b b个数对。
3.2 样例分析
如上所述。
4 解法分析
这是一道建图+搜索的简单题目。
通过题目,很容易可以按题意建图。对于操作2,我们可以通过搜索来判断是否可以到达。
AC代码
ACCode #001
// From andrey-tsb
// Rating 2010
// reason : 思路清晰,代码简洁,运用深搜。
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <deque>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <cctype>
#include <algorithm>
using namespace std;
vector<vector<int> > gr;
char used[110];
void dfs(int v)
{
used[v] = true;
for (int i=0;i<gr[v].size();i++)
{
int to = gr[v][i];
if (!used[to])
dfs(to);
}
}
int main()
{
int n;
cin >> n;
vector<pair<int,int> > mas;
for (int i=0;i<n;i++)
{
int a,b,c;
cin >> a >> b >> c;
if (a == 1)
{
mas.push_back(make_pair(b,c));
gr.push_back(vector<int>());
for (int j=0;j+1<mas.size();j++)
{
if (mas[j].first > mas.back().first && mas[j].first < mas.back().second ||
mas[j].second > mas.back().first && mas[j].second < mas.back().second)
gr[j].push_back(mas.size()-1);
if (mas.back().first > mas[j].first && mas.back().first < mas[j].second ||
mas.back().second > mas[j].first && mas.back().second < mas[j].second)
gr.back().push_back(j);
}
}
else
{
memset(used,0,sizeof(used));
dfs(b-1);
if (used[c-1])
cout << "YES\n";
else
cout << "NO\n";
}
}
return 0;
}
ACCode #002
// From Heart_Blue
// Rating 2425
// reason : 思路清晰,代码简洁,运用了广搜。
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MEM(a,b) memset((a),(b),sizeof(a))
const int INF = 0x3f3f3f3f;
int a[101];
int b[101];
int c[101][101];
int m;
bool check(int x, int y)
{
if(a[x] > a[y] && a[x] < b[y]) return true;
if(b[x] > a[y] && b[x] < b[y]) return true;
return false;
}
void check2(int t)
{
list<int> l;
l.push_back(t);
int flag[101];
MEM(flag,0);
flag[t] = 1;
while(!l.empty())
{
int x = l.front();
l.pop_front();
for(int i = 1; i < m; i++)
{
if(flag[i]) continue;
if(c[x][i])
{
l.push_back(i);
c[t][i] = 1;
flag[i] = 1;
}
}
}
}
int main()
{
// freopen("input.txt","r",stdin);
int n;
MEM(c,0);
cin >> n;
int q,x,y;
m = 1;
for(int i = 0; i < n; i++)
{
cin >> q >> x >> y;
if(q == 1)
{
a[m] = x;
b[m] = y;
for(int j = 1; j < m; j++)
{
if(check(j,m))
{
c[j][m] = 1;
}
if(check(m,j))
{
c[m][j] = 1;
}
}
m++;
}
if(q == 2)
{
check2(x);
if(c[x][y])
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
return 0;
}
ACCode #003
// From cnnfls_csy
// Rating 2937
// reason : 思路清晰,代码简洁明了,运用了dfs,代码不拖油带水
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
using namespace std;
int n,m,i,j,x,y,k,b[1005],c[1005],d[1005];
void dfs(int x)
{
d[x]=1;
for (int i=1;i<=m;i++)
if (!d[i]&&((b[i]<b[x]&&b[x]<c[i])||(b[i]<c[x]&&c[x]<c[i])))
dfs(i);
}
int main()
{
cin>>n;
for (j=1;j<=n;j++)
{
cin>>k>>x>>y;
if (k==1)
{
m++;
b[m]=x;c[m]=y;
}
else
{
for (i=1;i<=m;i++) d[i]=0;
dfs(x);
if (d[y]) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return 0;
}