Skip to content

Commit a3a254c

Browse files
committed
Problem 4 DSU fucked
1 parent 602e641 commit a3a254c

File tree

3 files changed

+275
-0
lines changed

3 files changed

+275
-0
lines changed

CP/January Lunchtime 2018/prob1.py

Whitespace-only changes.

CP/JanuaryCookOff2018/prob3-dp.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
typedef long long int ll;
5+
const ll inf=pow(10,14);
6+
bool apply(ll x,ll y,ll op)
7+
{
8+
if(op==0)
9+
{
10+
return x<y;
11+
}
12+
return x>y;
13+
}
14+
15+
bool apply2(ll x,ll y,ll op)
16+
{
17+
if(op==1)
18+
{
19+
return x<y;
20+
}
21+
return x>y;
22+
}
23+
24+
int main(){
25+
26+
ll t;
27+
cin>>t;
28+
while(t--)
29+
{
30+
ll n,arr[100005];
31+
cin>>n;
32+
for(ll i=0;i<n;i++)
33+
cin>>arr[i];
34+
35+
ll dp[100005][2];
36+
for(ll i=0;i<100005;i++)
37+
{
38+
for(ll j=0;j<2;j++)
39+
dp[i][j]=inf;
40+
}
41+
42+
if(n==2)
43+
{
44+
if(arr[0]==arr[1])
45+
cout<<-1<<endl;
46+
else
47+
cout<<0<<endl;
48+
continue;
49+
}
50+
51+
ll x=ceil(1.0*n/2);
52+
dp[0][0]=0,dp[0][1]=1;
53+
for(ll i=1;i<x;i++)
54+
{
55+
ll val1=dp[i-1][0],val2=dp[i-1][1];
56+
if(!(apply(arr[i-1],arr[i],i%2) && apply(arr[n-i-1],arr[n-i],(n-i)%2)))
57+
{
58+
val1=inf;
59+
}
60+
if(!(apply(arr[n-i],arr[i],i%2) && apply(arr[n-i-1],arr[i-1],(n-i)%2)))
61+
{
62+
val2=inf;
63+
}
64+
dp[i][0]=min(val1,val2);
65+
66+
val1=dp[i-1][0],val2=dp[i-1][1];
67+
if(!(apply(arr[i-1],arr[n-i-1],i%2) && apply(arr[i],arr[n-i],(n-i)%2)))
68+
{
69+
val1=inf;
70+
}
71+
if(!(apply(arr[n-i],arr[n-i-1],i%2) && apply(arr[i],arr[i-1],(n-i)%2)))
72+
{
73+
val2=inf;
74+
}
75+
dp[i][1]=min(val1,val2)+1;
76+
}
77+
78+
ll dp2[100005][2];
79+
for(ll i=0;i<100005;i++)
80+
{
81+
for(ll j=0;j<2;j++)
82+
dp2[i][j]=inf;
83+
}
84+
85+
86+
dp2[0][0]=0,dp2[0][1]=1;
87+
for(ll i=1;i<x;i++)
88+
{
89+
ll val1=dp2[i-1][0],val2=dp2[i-1][1];
90+
if(!(apply2(arr[i-1],arr[i],i%2) && apply2(arr[n-i-1],arr[n-i],(n-i)%2)))
91+
{
92+
val1=inf;
93+
}
94+
if(!(apply2(arr[n-i],arr[i],i%2) && apply2(arr[n-i-1],arr[i-1],(n-i)%2)))
95+
{
96+
val2=inf;
97+
}
98+
dp2[i][0]=min(val1,val2);
99+
100+
val1=dp2[i-1][0],val2=dp2[i-1][1];
101+
if(!(apply2(arr[i-1],arr[n-i-1],i%2) && apply2(arr[i],arr[n-i],(n-i)%2)))
102+
{
103+
val1=inf;
104+
}
105+
if(!(apply2(arr[n-i],arr[n-i-1],i%2) && apply2(arr[i],arr[i-1],(n-i)%2)))
106+
{
107+
val2=inf;
108+
}
109+
dp2[i][1]=min(val1,val2)+1;
110+
}
111+
ll res=min(min(dp[x-1][0],dp[x-1][1]),min(dp2[x-1][0],dp2[x-1][1]));
112+
if(res>pow(10,12))
113+
cout<<-1<<endl;
114+
else
115+
cout<<res<<endl;
116+
}
117+
return 0;
118+
}

CP/JanuaryCookOff2018/prob4.cpp

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
const int N = 1005;
5+
6+
int A[N][N];
7+
vector<int> adj[N];
8+
bool visited[N];
9+
int ans[N][N];
10+
bool myParity[N];
11+
12+
int p[N], sz[N];
13+
14+
int par(int x){
15+
if(p[x] != x) p[x] = par(p[x]);
16+
return p[x];
17+
}
18+
19+
void join(int a, int b){
20+
int p1 = par(a), p2 = par(b);
21+
if(p1 == p2) return ;
22+
if(sz[p2] > sz[p1]) swap(p1, p2);
23+
sz[p1] += sz[p2];
24+
p[p2] = p1;
25+
}
26+
27+
bool BipartiteCheck(int u, int parity) {
28+
visited[u] = 1;
29+
myParity[u] = parity;
30+
for(auto v : adj[u]) {
31+
if(A[u][v] == 1) continue;
32+
if(visited[v] == 1 and myParity[v] == myParity[u]) return 0;
33+
}
34+
for(auto v : adj[u]){
35+
if(visited[v] == 1 or A[u][v] == 1) continue;
36+
if(BipartiteCheck(v, parity^1) == 0) return 0;
37+
}
38+
return 1;
39+
}
40+
41+
void dfs_assign(int u, int val, int col_number) {
42+
visited[u] = 1;
43+
ans[u][col_number] = val;
44+
for(auto v : adj[u]) {
45+
if(visited[v]) continue;
46+
dfs_assign(v, val * A[u][v], col_number);
47+
}
48+
}
49+
50+
void solve() {
51+
int n;
52+
scanf("%d", &n);
53+
//assert(n >= 1 and n <= 1000);
54+
for(int i = 1; i <= n; i++) {
55+
for(int j = 1; j <= n; j++) {
56+
scanf("%d", &A[i][j]);
57+
assert(A[i][j] >= -1 and A[i][j] <= 1);
58+
}
59+
}
60+
61+
for(int i = 1; i <= n; i++) {
62+
p[i] = i, sz[i] = 1;
63+
visited[i] = 0;
64+
adj[i].clear();
65+
for(int j = 1; j <= n; j++) ans[i][j] = 0;
66+
}
67+
68+
for(int i = 1; i <= n; i++) {
69+
for(int j = 1; j <= n; j++) {
70+
if(A[i][j] != A[j][i]) {
71+
cout<<-1<<endl;
72+
//cout<<"non symmetric"<<endl;
73+
return ;
74+
}
75+
}
76+
}
77+
78+
// ^ensured matrix is symmetric.
79+
80+
for(int i = 1; i <= n; i++) {
81+
if(A[i][i] == -1) {
82+
cout<<-1<<endl;
83+
//cout<<"diagonal fucked"<<endl;
84+
return ;
85+
}
86+
}
87+
88+
// ^ensured none of the diagonal entries is -1.
89+
90+
for(int i = 1; i <= n; i++) {
91+
for(int j = i + 1; j <= n; j++) {
92+
if(A[i][j] != 0) {
93+
if(A[i][i] == 0 or A[j][j] == 0) {
94+
cout<<-1<<endl;
95+
//cout<<"involve zero row"<<endl;
96+
return ;
97+
}
98+
join(i, j);
99+
adj[i].push_back(j);
100+
adj[j].push_back(i);
101+
}
102+
}
103+
}
104+
105+
// ^joined the rows, which have non - zero entries in dsu
106+
// and added edges between those rows which have opposite parities.
107+
// also verified that no pure zero row is involved.
108+
109+
for(int i = 1; i <= n; i++) {
110+
for(int j = i + 1; j <= n; j++) {
111+
if(A[i][j] == 0 and par(i) == par(j)) {
112+
cout<<-1<<endl;
113+
//cout<<"non - transitive"<<endl;
114+
return ;
115+
}
116+
}
117+
}
118+
119+
//cout<<"BC"<<endl;
120+
121+
// ^ensured that all the pair of rows which are not connected are not indirectly in the same dsu.
122+
123+
for(int i = 1; i <= n; i++) {
124+
if(visited[i]) continue;
125+
if(!BipartiteCheck(i, 0)) {
126+
cout<<-1<<endl;
127+
//cout<<"non - bipartite"<<endl;
128+
return ;
129+
}
130+
}
131+
132+
// ^ensured there are no parity issues and that all the graphs are bipartite in nature.
133+
134+
for(int i = 1; i <= n; i++) {
135+
visited[i] = (A[i][i] == 0);
136+
}
137+
138+
int colNumber = 1;
139+
140+
for(int i = 1; i <= n; i++) {
141+
if(visited[i]) continue;
142+
dfs_assign(i, -1, colNumber++);
143+
}
144+
145+
for(int i = 1; i <= n; i++) {
146+
for(int j = 1; j <= n; j++) {
147+
cout<<ans[i][j]<<" ";
148+
}
149+
cout<<endl;
150+
}
151+
}
152+
153+
int main() {
154+
int t;
155+
scanf("%d", &t);
156+
while(t--) solve();
157+
}

0 commit comments

Comments
 (0)