-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
128 lines (115 loc) · 3.26 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Linq;
namespace EvenTree
{
class Program
{
static void Main(string[] args)
{
int[] n = new int[2];
n = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
Node[] nodes = new Node[n[0]];
for (int i = 0; i < nodes.Length; i++) nodes[i] = new Node();
for (int i = 0; i < n[1]; i++)
{
int[] e = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
nodes[e[0] - 1].AddEdge(nodes[e[1] - 1]);
}
int maxCut = 0;
for(int i = 0; i < nodes.Length; i++)
{
if(nodes[i].GetParentID() != 0)
{
int t = nodes[i].GetParentID();
nodes[i].RemoveEdge();
if (nodes[i].ForestSize % 2 == 0 && nodes[t - 1].ForestSize % 2 == 0) maxCut++;
else nodes[i].AddEdge(nodes[t - 1]);
}
}
Console.WriteLine(maxCut);
}
}
class Node
{
static int nodes = 0;
int id { get; set; }
int forestSize { get; set; }
Node parent = null;
List<Node> children = new List<Node>();
public Node() : this(null) { }
public Node(Node _parent)
{
parent = _parent;
nodes++;
ID = nodes;
ForestSize = 1;
}
public int ID
{
get { return id; }
set { id = value; }
}
public int ForestSize
{
get { return forestSize; }
set
{
forestSize = value;
if(parent != null)
{
parent.ForestSize = value;
}
foreach(Node i in children)
{
i.forestSize = value;
}
}
}
public int GetParentID()
{
if (parent != null) return parent.ID;
else return 0;
}
public void AddEdge(Node _parent)
{
parent = _parent;
parent.AddChildren(this);
CalculateForest();
}
public void RemoveEdge()
{
Node op = parent;
parent.RemoveChildren(this);
parent = null;
CalculateForest();
op.CalculateForest();
}
void AddChildren(Node child)
{
if(children.All(x => x.ID != child.ID)) children.Add(child);
foreach(Node i in child.children)
{
if (children.All(x => x.ID != i.ID)) children.Add(i);
}
if(parent != null)
{
parent.AddChildren(this);
}
}
void RemoveChildren(Node child)
{
children.Remove(child);
foreach(Node i in child.children)
{
children.Remove(i);
}
if(parent != null) parent.RemoveChildren(child);
}
void CalculateForest()
{
if (parent == null) ForestSize = children.Count + 1;
else parent.CalculateForest();
}
}
}