ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1651. Shortest Subchain

WA 15
Posted by LastOne 30 Aug 2017 07:49

#include <bits/stdc++.h>
using namespace std;
const int infInt = 1<<30;
pair<int,int> edges[100010];
int parent[100010],dist[100010];
map< pair<int,int> ,int> Map;
bool mark[100010];

void path(int u){
if(parent[u]==u){
cout<<edges[u].first<<" "<<edges[u].second;
return;
}
path(parent[u]);
cout<<" "<<edges[u].second;
}
int main(){
int ant,curr,root,n;
cin>>n;
for(int i=0;i<n;i++){
cin>>curr;
if(i>0){
edges[i].first=ant;
edges[i].second=curr;
Map[make_pair(ant,curr)]=i;
}

if(i==0)
root=curr;
ant=curr;
}
if(root==curr){
cout<<root<<endl;
return 0;
}
queue<int> q;
for(int i=1;i<n;i++){
parent[i]=i;
dist[i]=infInt;
if(edges[i].first==root){
q.push(i);
dist[i]=0;
}
if(edges[i].second==curr)
mark[i]=true;
}

while(!q.empty()){
int u=q.front();
q.pop();
int x=edges[u].first;
int y=edges[u].second;
int v=Map[make_pair(y,z)];
if(v>u and dist[v]>dist[u]+1){
dist[v]=dist[u]+1;
parent[v]=u;
q.push(v);
}
}
}
int best=infInt,bestInd=-1;
for(int i=1;i<n;i++){
if(dist[i]<best and mark[i]){
bestInd=i;
best=dist[i];
}
}
path(bestInd);
cout<<endl;
return 0;
}
Re: WA 15
Posted by LastOne 30 Aug 2017 08:51
Or this, aldo wa 15
What´s the problem??

#include <bits/stdc++.h>
using namespace std;
const int infInt = 1e9;

pair<int,int> edges[100010];
int final;
map< pair<int,int> ,int> Map;
bool mark[100010];

bool vis[100010];
int dp[100010];

int f(int u){
if(edges[u].second==final)return 0;
if(vis[u])return dp[u];
vis[u]=true;
int x=edges[u].first;
int y=edges[u].second;
int ans=infInt;
int v=Map[make_pair(y,z)];
if(v>u)
ans=min(ans,1+f(v));
}
return dp[u]=ans;
}

void rec(int u){
if(edges[u].second==final){
cout<<" "<<final<<endl;
return;
}
int x=edges[u].first;
int y=edges[u].second;
int v=Map[make_pair(y,z)];
if(v>u and dp[u]==1+f(v)){
cout<<" "<<y;
rec(v);
break;
}
}
}

int main(){
int ant,curr,root,n;
cin>>n;
for(int i=0;i<n;i++){
cin>>curr;
if(i>0){
edges[i].first=ant;
edges[i].second=curr;
Map[make_pair(ant,curr)]=i;
}

if(i==0)
root=curr;
ant=curr;
if(i==n-1)
final=curr;
}
if(root==curr){
cout<<root<<endl;
return 0;
}
int best=infInt,bestInd=-1;
for(int i=1;i<n;i++){
if(edges[i].first==root){
int ans=f(i);
if(ans < best){
best=ans;
bestInd=i;
}
}
}
cout<<edges[bestInd].first;
rec(bestInd);

}
LastOne wrote 30 August 2017 07:49

#include <bits/stdc++.h>
using namespace std;
const int infInt = 1<<30;
pair<int,int> edges[100010];
int parent[100010],dist[100010];
map< pair<int,int> ,int> Map;
bool mark[100010];

void path(int u){
if(parent[u]==u){
cout<<edges[u].first<<" "<<edges[u].second;
return;
}
path(parent[u]);
cout<<" "<<edges[u].second;
}
int main(){
int ant,curr,root,n;
cin>>n;
for(int i=0;i<n;i++){
cin>>curr;
if(i>0){
edges[i].first=ant;
edges[i].second=curr;
Map[make_pair(ant,curr)]=i;
}

if(i==0)
root=curr;
ant=curr;
}
if(root==curr){
cout<<root<<endl;
return 0;
}
queue<int> q;
for(int i=1;i<n;i++){
parent[i]=i;
dist[i]=infInt;
if(edges[i].first==root){
q.push(i);
dist[i]=0;
}
if(edges[i].second==curr)
mark[i]=true;
}

while(!q.empty()){
int u=q.front();
q.pop();
int x=edges[u].first;
int y=edges[u].second;
int v=Map[make_pair(y,z)];
if(v>u and dist[v]>dist[u]+1){
dist[v]=dist[u]+1;
parent[v]=u;
q.push(v);
}
}
}
int best=infInt,bestInd=-1;
for(int i=1;i<n;i++){
if(dist[i]<best and mark[i]){
bestInd=i;
best=dist[i];
}
}
path(bestInd);
cout<<endl;
return 0;
}