3 条题解
-
1
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <string> #include <cmath> #include <cstdlib> using namespace std; int t,n,f[1000007],book[1000007*3]; //t表示t组数据,n表示有n个操作,f[]是我们并查集的数字,book[]是离散化的数组 struct node{ int x,y,e; }a[1000001]; bool cmp(node a,node b){ return a.e>b.e; }//排 序将e==1的放在前面 inline void first(int kkk){ for(int i=1;i<=kkk;i++) f[i]=i; }//初 始 化 int get(int x){ if(x==f[x]) return x; return f[x]=get(f[x]); } int main(){ scanf("%d",&t); while(t--){ int tot=-1; memset(book,0,sizeof(book)); memset(a,0,sizeof(a)); memset(f,0,sizeof(f)); int flag=1; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].e); book[++tot]=a[i].x; book[++tot]=a[i].y; } sort(book,book+tot);//排序 int reu=unique(book,book+tot)-book; //去重 for(int i=1;i<=n;++i){ a[i].x=lower_bound(book,book+reu,a[i].x)-book; a[i].y=lower_bound(book,book+reu,a[i].y)-book; } first(reu); //初始化 sort(a+1,a+n+1,cmp); //按e排序 for(int i=1;i<=n;i++){ int r1=get(a[i].x); int r2=get(a[i].y); if(a[i].e){ f[r1]=r2; //就是我们的merge操作 }else if(r1==r2){ printf("NO\n"); flag=0; //如果不满足条件,标记为否 break; } } if(flag) printf("YES\n"); //都满足条件了 } return 0; }
-
0
的范围较大,将它们离散化,映射到 的整数即可。然后扫描所有相等的条件,合并;再扫描所有不等的条件,如果与前面的矛盾,则无法满足。
#include <iostream> #include <algorithm> using namespace std; struct Condition { long long i, j; bool equal; }; int fa[200005]; long long vars[200005]; Condition cond[100005]; int t, n, m; int query(long long x) { return lower_bound(vars + 1, vars + m + 1, x) - vars; } int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } void unite(int x, int y) { fa[find(x)] = find(y); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> t; while (t--) { cin >> n; for (int i = 1; i <= n; ++i) { Condition c {}; cin >> c.i >> c.j >> c.equal; cond[i] = c; vars[i * 2 - 1] = c.i, vars[i * 2] = c.j; } sort(vars + 1, vars + n * 2 + 1); m = unique(vars + 1, vars + n * 2 + 1) - vars - 1; for (int i = 1; i <= m; ++i) fa[i] = i; for (int i = 1; i <= n; ++i) if (cond[i].equal) unite(query(cond[i].i), query(cond[i].j)); bool valid = true; for (int i = 1; i <= n; ++i) if (!cond[i].equal && find(query(cond[i].i)) == find(query(cond[i].j))) { cout << "NO" << endl; valid = false; break; } if (valid) cout << "YES" << endl; } return 0; }
-
0
/* ID: Keqi Zhao TASK: test LANG: C++ */ #include <iostream> #include <cstdio> #include <algorithm> #define Maxn 1000010 using namespace std; int n, bt, ct, T, b[Maxn * 2], c[Maxn * 2], fa[Maxn * 2]; struct node{ int x, y, z; }a[Maxn]; int find (int x){ if(fa[x] == x) return x; return fa[x] = find(fa[x]); } void merge(int x, int y){ x = find(x); y = find(y); if(x != y) fa[x] = y; } int main() { cin >> T; while(T--){ cin >> n; bt = 0, ct = 0; for(int i = 1; i <= n; i ++){ cin >> a[i].x >> a[i].y >> a[i].z; b[++bt] = a[i].x; b[++bt] = a[i].y; } sort(b + 1, b + bt + 1); for(int i = 1; i <= bt; i++) if(b[i] != b[i - 1]) c[++ct] = b[i]; for(int i = 1; i <= bt; i++){ a[i].x = lower_bound(c + 1, c + ct + 1, a[i].x) - c; a[i].y = lower_bound(c + 1, c + ct + 1, a[i].y) - c; } for(int i = 1; i <= ct; i++) fa[i] = i; for(int i = 1; i <= n; i++) if(a[i].z) merge(a[i].x , a[i].y); bool flag = true; for(int i = 1; i <= n; i++) if(!a[i].z) if(find(a[i].x) == find(a[i].y)){ flag = false; break; } cout << (flag ? "YES" : "NO") << endl; } return 0; }
- 1
信息
- ID
- 918
- 时间
- 2000ms
- 内存
- 500MiB
- 难度
- 4
- 标签
- 递交数
- 15
- 已通过
- 7
- 上传者