Skip to content

Commit 3218844

Browse files
authored
Create 1684-Giant Pizza.cpp
2SAT problem https://cses.fi/book/book.pdf page no - 160-162
1 parent b42ec3e commit 3218844

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

cses graph/1684-Giant Pizza.cpp

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// JAI BAJARANG BALI
2+
3+
// manitianajay45
4+
5+
// give me some sunshine, give me some rain , give me another chance to grow up once again....
6+
7+
// sab moh maya hai....
8+
9+
/*
10+
2SAT problem
11+
https://cses.fi/book/book.pdf
12+
page no - 160-162
13+
/*
14+
15+
16+
17+
#include <bits/stdc++.h>
18+
using namespace std;
19+
20+
#define ll long long
21+
#define mod 1000000007
22+
ll n,m;
23+
24+
bool visited[200005][2];
25+
string ans(200005,'*');
26+
vector<pair<ll,ll>> graph[200005][2];
27+
vector<pair<ll,ll>> order;
28+
vector<pair<ll,ll>> graphr[200005][2];
29+
ll vis[200005][2];
30+
ll an=0;
31+
32+
33+
void topo(ll v,ll tp){
34+
visited[v][tp]=true;
35+
for(auto u:graph[v][tp]){
36+
if(!visited[u.first][u.second]){
37+
topo(u.first,u.second);
38+
}
39+
}
40+
order.push_back({v,tp});
41+
}
42+
bool flag=true;
43+
44+
void dfs(ll v,ll tp){
45+
46+
47+
if(tp==0 && vis[v][1]==an){
48+
flag=false;
49+
}
50+
if(tp==1 && vis[v][0]==an){
51+
flag=false;
52+
}
53+
vis[v][tp]=an;
54+
if(ans[v]=='*'){
55+
if(tp==0){
56+
ans[v]='+';
57+
}else{
58+
ans[v]='-';
59+
}
60+
}
61+
62+
for(auto u:graphr[v][tp]){
63+
if(vis[u.first][u.second]==0){
64+
dfs(u.first,u.second);
65+
}
66+
}
67+
68+
}
69+
70+
71+
72+
int main()
73+
{
74+
ios_base::sync_with_stdio(false);
75+
cin.tie(NULL);
76+
77+
// ll n;
78+
cin >> n>>m;
79+
80+
for (ll i = 0; i <n; i++)
81+
{
82+
ll u,v;
83+
char ui,vi;
84+
cin>>ui>>u>>vi>>v;
85+
86+
ll fui,fvi;
87+
fui=(ui=='+')?1:0;
88+
fvi=(vi=='+')?1:0;
89+
if(fui==0){
90+
graph[u][1].push_back({v,fvi});
91+
graphr[v][fvi].push_back({u,1});
92+
}else{
93+
graph[u][0].push_back({v,fvi});
94+
graphr[v][fvi].push_back({u,0});
95+
}
96+
if(fvi==0){
97+
graph[v][1].push_back({u,fui});
98+
graphr[u][fui].push_back({v,1});
99+
}else{
100+
graph[v][0].push_back({u,fui});
101+
graphr[u][fui].push_back({v,0});
102+
}
103+
104+
105+
}
106+
for(ll i=1;i<=m;i++){
107+
if(!visited[i][0]){
108+
topo(i,0);
109+
}
110+
if(!visited[i][1]){
111+
topo(i,1);
112+
}
113+
}
114+
115+
reverse(order.begin(),order.end());
116+
117+
for(auto i:order){
118+
if(vis[i.first][i.second]==0){
119+
an++;
120+
dfs(i.first,i.second);
121+
if(!flag){
122+
break;
123+
}
124+
// an++;
125+
}
126+
}
127+
if(!flag){
128+
cout<<"IMPOSSIBLE"<<endl;
129+
return 0;
130+
}
131+
for(ll i=1;i<=m;i++){
132+
cout<<ans[i];
133+
}
134+
cout<<endl;
135+
136+
137+
return 0;
138+
}

0 commit comments

Comments
 (0)