Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STRINGRA (AUG17) [CPP] #254

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 2017/AUG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
| [AUG17](https://www.codechef.com/AUG17) | [GCAC](https://www.codechef.com/AUG17/problems/GCAC) | ★★★ | | [![image](../img/GH.png)](AUG/AUG17/GCAC/GCAC.cpp) [![image](../img/CC.png)](https://www.codechef.com/viewsolution/14947059) (100 pts) [![image](../img/AC.png)](#) | | |
| [AUG17](https://www.codechef.com/AUG17) | [PALINGAM](https://www.codechef.com/AUG17/problems/PALINGAM) | ★★★ | | [![image](../img/GH.png)](AUG/AUG17/PALINGAM/PALINGAM.cpp) [![image](../img/CC.png)](https://www.codechef.com/viewsolution/14908284) (100 pts) [![image](../img/AC.png)](#) | | |
| [AUG17](https://www.codechef.com/AUG17) | [CHEFFA](https://www.codechef.com/AUG17/problems/CHEFFA) | ★★★★ | | | | |
| [AUG17](https://www.codechef.com/AUG17) | [STRINGRA](https://www.codechef.com/AUG17/problems/STRINGRA) | ★★★★ | | | | |
| [AUG17](https://www.codechef.com/AUG17) | [STRINGRA](https://www.codechef.com/AUG17/problems/STRINGRA) | ★★★★ | | [![image](../img/GH.png)](AUG/AUG17/STRINGRA/STRINGRA.cpp) [![image](../img/CC.png)](https://www.codechef.com/viewsolution/14982151) (100 pts) [![image](../img/AC.png)](#) | | |
| [AUG17](https://www.codechef.com/AUG17) | [MATDW](https://www.codechef.com/AUG17/problems/MATDW) | ★★★★ | | | | |
| [AUG17](https://www.codechef.com/AUG17) | [HILLJUMP](https://www.codechef.com/AUG17/problems/HILLJUMP) | ★★★★★ | | [![image](../img/GH.png)](AUG/AUG17/HILLJUMP/HILLJUMP.cpp) [![image](../img/CC.png)](https://www.codechef.com/viewsolution/14996982) (100 pts) [![image](../img/AC.png)](#) | | |
| [AUG17](https://www.codechef.com/AUG17) | [WALKBT](https://www.codechef.com/AUG17/problems/WALKBT) | ★★★★★ | | | | |
Expand Down
94 changes: 94 additions & 0 deletions 2017/AUG/AUG17/STRINGRA/stringra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <bits/stdc++.h>

using namespace std;

int main(){
int t;
cin>>t;
while(t--){
int n,m;

cin>>n>>m;
vector<int> ad[n+1];
while(m--){
int a,b;

cin>>a>>b;

if(a<b){
ad[a].push_back(b);
}
}

int arr[n+1]={-1,0};
int edges = ad[0].size();
int inital[edges+1];
for(int i=1;i<=edges;i++){
inital[i] = INT_MAX;
}

sort(ad[0].begin(),ad[0].end());

for(int i=0;i<ad[0].size();i++){
arr[ad[0][i]] = i+1;
inital[i+1] = ad[0][i];
}

bool avai[edges+1];
memset(avai,1,edges+1);

for(int i=1;i<n;i++){
int temp = -1, idx;
if(ad[i].size()>edges||ad[i].size()==0){
goto re;
}

for(int j=0;j<ad[i].size();++j){
idx = ad[i][j];
if(arr[idx]==0){
if(temp==-1){
temp = idx;
}

else
goto re;
}
}
if(temp!=-1){
for(int k=1;k<=edges;k++){
if(avai[k]&&inital[k]<=i){
arr[temp] = k;
inital[k] = temp;
break;
}
}
}

memset(avai,0,edges+1);

for(int j=0;j<ad[i].size();j++) {
avai[arr[ad[i][j]]]=1;
}
}

for(int i=0;i<=n;i++){
int count = 0;
for(int j=1;j<=edges;++j){
if(inital[j]>i){
++count;
}
}
if(count!=ad[i].size()){
goto re;
}
}
for(int i=1;i<=n;i++){
cout<<arr[i]<<' ';
}

cout<<endl;
continue;
re:;
cout<<-1<<endl;
}
}