Submission #7635238
Source Code Expand
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace CF16_final_C
{
class Program
{
public static void Solve(Input input)
{
var n = input.NextInt();
var m = input.NextInt();
var used = new bool[m];
var uf = new UnionFind(m);
for (int i = 0; i < n; i++)
{
var k = input.NextInt();
var l = input.NextInt(k, -1);
for (int j = 0; j < l.Length; j++)
{
uf.Merge(l[0], l[j]);
used[l[j]] = true;
}
}
var usedCount = used.Count(x => x);
for (int i = 0; i < m; i++)
{
if (!used[i])
{
continue;
}
if (uf.SameCount(i) != usedCount)
{
Console.WriteLine("NO");
return;
}
}
Console.WriteLine("YES");
}
/// <summary>
/// 重み付きUF木
/// </summary>
class UnionFind
{
int[] Rank { get; set; }
int[] Size { get; set; }
int[] ParentId { get; set; }
int[] DiffWeight { get; set; }
/// <summary>
///
/// </summary>
/// <param name="size">ノードの総数</param>
public UnionFind(int size)
{
this.Rank = new int[size];
this.Size = Enumerable.Repeat(1, size).ToArray();
this.ParentId = Enumerable.Range(0, size).ToArray();
this.DiffWeight = new int[size];
}
/// <summary>
/// 同一の親を持つか
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public bool Same(int x, int y)
{
return FindRoot(x) == FindRoot(y);
}
/// <summary>
/// 指定ノードのrootを返す
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
public int FindRoot(int x)
{
if (x != this.ParentId[x])
{
// 経路中のノードのパラメータを更新しておく
var root = FindRoot(this.ParentId[x]);
this.DiffWeight[x] += this.DiffWeight[this.ParentId[x]];
this.ParentId[x] = root;
}
return this.ParentId[x];
}
/// <summary>
/// 親経路との重さを返す
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
public int Weight(int x)
{
FindRoot(x);
return this.DiffWeight[x];
}
/// <summary>
/// x,yが同一の親を持つ場合、Weightの差分(y-x)を返す
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int Weight(int x, int y)
{
return Weight(y) - Weight(x);
}
/// <summary>
/// 重みをつけて合体する
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="w"></param>
public void Merge(int x, int y, int w = 0)
{
w += Weight(x) - Weight(y);
x = FindRoot(x);
y = FindRoot(y);
if (x == y) return;
if (this.Rank[x] < this.Rank[y])
{
this.ParentId[x] = y;
this.Size[y] += this.Size[x];
this.DiffWeight[x] = -w;
}
else
{
this.ParentId[y] = x;
this.Size[x] += this.Size[y];
this.DiffWeight[y] = w;
if (this.Rank[x] == this.Rank[y])
{
this.Rank[x]++;
}
}
}
/// <summary>
/// xと同じ親を持つノードの数
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
public int SameCount(int x)
{
x = FindRoot(x);
return this.Size[x];
}
}
#region Main
public static void Main(string[] args)
{
// 出力が少ないときはこれをセットする方が時間かかるけれど
// そのときにTLEするのはアルゴリズムが悪いせいだし、まあ良しとする
var needsFlushOutput = true;
if (needsFlushOutput)
{
// 細かく出力しないようにする
var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
Console.SetOut(sw);
}
// 通常は引数無しで、ファイルから読み出したい場合はpath指定する
var input = new Input();
// 仮想的に標準入力をセットする
// テスト入力したまま提出することがままあるので……
#if DEBUG
input.SetText("");
#endif
Solve(input);
Console.Out.Flush();
}
#endregion
#region Competitive Template
#pragma warning disable CS0414
static readonly int MOD = (int)1e9 + 7;
static readonly int[] dx = { 1, 0, -1, 0 };
static readonly int[] dy = { 0, 1, 0, -1 };
/// <summary> 左上が原点 </summary>
static readonly char[] dir = { 'R', 'U', 'L', 'D' };
#pragma warning restore CS0414
public class Input
{
// 変な改行コードがたまに混じっているので、一応セパレート指定する
// スペース単独指定の方がもちろん早い
static readonly char[] separator = { ' ', '\r', '\n' };
readonly StreamReader sr;
readonly Queue<string> queue;
/// <summary>
/// 特定のファイルから読み出したい場合はpath指定する
/// </summary>
public Input(string path = "")
{
queue = new Queue<string>();
if (string.IsNullOrEmpty(path)) { sr = new StreamReader(Console.OpenStandardInput()); }
else { sr = new StreamReader(path); }
}
/// <summary>
/// 入力予約
/// </summary>
public void SetText(IEnumerable<string> items)
{
foreach (var item in items)
SetText(item);
}
/// <summary>
/// 入力予約
/// </summary>
public bool SetText(string s)
{
if (string.IsNullOrEmpty(s)) return false;
foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
queue.Enqueue(elem);
return true;
}
/// <summary>
/// 要素が存在するか
/// </summary>
public bool Any() => queue.Any() || Read();
/// <summary>
/// 内部queueに入力からの値をsplitして格納する
/// </summary>
bool Read()
{
if (!SetText(sr.ReadLine())) return false;
if (!queue.Any()) return Read();
return queue.Any();
}
/// <summary>
/// 次のstringを一つ読み込む
/// </summary>
public string Next()
{
if (!queue.Any() && !Read()) return "";
return queue.Dequeue();
}
/// <summary>
/// 指定個数queueにたまるまでenqueueし続ける
/// </summary>
bool Accumulate(int n)
{
while (queue.Count() < n)
if (!Read()) return false;
return true;
}
public int NextInt() => int.Parse(Next());
public long NextLong() => long.Parse(Next());
public double NextDouble() => double.Parse(Next());
/// <summary>
/// n個の要素をparseして、それぞれにoffsetをaddした配列を返す
/// </summary>
T[] NextT<T>(int n, T offset, Func<string, T> parse, Func<T, T, T> add)
{
if (!Accumulate(n)) return null;
var a = new T[n];
for (int i = 0; i < n; i++)
a[i] = add(parse(queue.Dequeue()), offset);
return a;
}
public string[] Next(int n) => NextT(n, "", x => x, (x, y) => x);
public int[] NextInt(int n, int offset = 0) => NextT(n, offset, int.Parse, (x, y) => x + y);
public long[] NextLong(int n, long offset = 0) => NextT(n, offset, long.Parse, (x, y) => x + y);
public double[] NextDouble(int n, double offset = 0.0) => NextT(n, offset, double.Parse, (x, y) => x + y);
}
static class Utils
{
public static T Max<T>(params T[] objs) => objs.Max();
public static T Min<T>(params T[] objs) => objs.Min();
/// <summary>
/// vでfillされたT[d1][d2]配列を作成する
/// </summary>
public static T[][] Create2DArray<T>(int d1, int d2, T v)
{
return
Enumerable.Repeat(0, d1).Select(_ =>
Enumerable.Repeat(v, d2).ToArray()).ToArray();
}
/// <summary>
/// vでfillされたT[d1][d2][d3]配列を作成する
/// </summary>
public static T[][][] Create3DArray<T>(int d1, int d2, int d3, T v)
{
return
Enumerable.Repeat(0, d1).Select(_ =>
Enumerable.Repeat(0, d2).Select(__ =>
Enumerable.Repeat(v, d3).ToArray()).ToArray()).ToArray();
}
}
#endregion
}
}
Submission Info
Submission Time |
|
Task |
C - Interpretation |
User |
tetora1011 |
Language |
C# (Mono 4.6.2.0) |
Score |
400 |
Code Size |
11120 Byte |
Status |
AC |
Exec Time |
134 ms |
Memory |
23764 KB |
Judge Result
Set Name |
sample |
dataset1 |
dataset2 |
Score / Max Score |
0 / 0 |
200 / 200 |
200 / 200 |
Status |
|
|
|
Set Name |
Test Cases |
sample |
sample-01.txt, sample-02.txt |
dataset1 |
sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt |
dataset2 |
sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, sample-01.txt, sample-02.txt |
Case Name |
Status |
Exec Time |
Memory |
01-01.txt |
AC |
29 ms |
13396 KB |
01-02.txt |
AC |
28 ms |
9300 KB |
01-03.txt |
AC |
28 ms |
9300 KB |
01-04.txt |
AC |
29 ms |
11348 KB |
01-05.txt |
AC |
28 ms |
9300 KB |
01-06.txt |
AC |
28 ms |
11348 KB |
01-07.txt |
AC |
28 ms |
13396 KB |
01-08.txt |
AC |
28 ms |
11348 KB |
01-09.txt |
AC |
28 ms |
11348 KB |
01-10.txt |
AC |
29 ms |
11348 KB |
02-01.txt |
AC |
82 ms |
19040 KB |
02-02.txt |
AC |
125 ms |
15540 KB |
02-03.txt |
AC |
81 ms |
17120 KB |
02-04.txt |
AC |
117 ms |
19040 KB |
02-05.txt |
AC |
119 ms |
15712 KB |
02-06.txt |
AC |
114 ms |
19040 KB |
02-07.txt |
AC |
126 ms |
17760 KB |
02-08.txt |
AC |
121 ms |
15412 KB |
02-09.txt |
AC |
134 ms |
19040 KB |
02-10.txt |
AC |
105 ms |
21720 KB |
02-11.txt |
AC |
103 ms |
23764 KB |
02-12.txt |
AC |
105 ms |
21204 KB |
02-13.txt |
AC |
103 ms |
21072 KB |
sample-01.txt |
AC |
28 ms |
13396 KB |
sample-02.txt |
AC |
27 ms |
11348 KB |