Problem: Problem F Pizza Delivery

Alyssa is a college student, living in New Tsukuba City. All the streets in the city are one-way. A new social experiment starting tomorrow is on alternative traffic regulation reversing the one-way directions of street sections. Reversals will be on one single street section between two adjacent intersections for each day; the directions of all the other sections will not change, and the reversal will be canceled on the next day.

Alyssa orders a piece of pizza everyday from the same pizzeria. The pizza is delivered along the shortest route from the intersection with the pizzeria to the intersection with Alyssa's house.

Altering the traffic regulation may change the shortest route. Please tell Alyssa how the social experiment will affect the pizza delivery route.

Input

The input consists of a single test case in the following format.


$n$ $m$
$a_1$ $b_1$ $c_1$
...
$a_m$ $b_m$ $c_m$


The first line contains two integers, $n$, the number of intersections, and $m$, the number of street sections in New Tsukuba City ($2 \leq n \leq 100 000, 1 \leq m \leq 100 000$). The intersections are numbered $1$ through $n$ and the street sections are numbered $1$ through $m$.

The following $m$ lines contain the information about the street sections, each with three integers $a_i$, $b_i$, and $c_i$ ($1 \leq a_i n, 1 \leq b_i \leq n, a_i \ne b_i, 1 \leq c_i \leq 100 000$). They mean that the street section numbered $i$ connects two intersections with the one-way direction from $a_i$ to $b_i$, which will be reversed on the $i$-th day. The street section has the length of $c_i$. Note that there may be more than one street section connecting the same pair of intersections.

The pizzeria is on the intersection 1 and Alyssa's house is on the intersection 2. It is guaranteed that at least one route exists from the pizzeria to Alyssa's before the social experiment starts.

Output

The output should contain $m$ lines. The $i$-th line should be

* HAPPY if the shortest route on the $i$-th day will become shorter,
* SOSO if the length of the shortest route on the $i$-th day will not change, and
* SAD if the shortest route on the $i$-th day will be longer or if there will be no route from the pizzeria to Alyssa's house.



Alyssa doesn't mind whether the delivery bike can go back to the pizzeria or not.

Sample Input 1


4 5
1 3 5
3 4 6
4 2 7
2 1 18
2 3 12


Sample Output 1


SAD
SAD
SAD
SOSO
HAPPY


Sample Input 2


7 5
1 3 2
1 6 3
4 2 4
6 2 5
7 5 6


Sample Output 2


SOSO
SAD
SOSO
SAD
SOSO


Sample Input 3


10 14
1 7 9
1 8 3
2 8 4
2 6 11
3 7 8
3 4 4
3 2 1
3 2 7
4 8 4
5 6 11
5 8 12
6 10 6
7 10 8
8 3 6


Sample Output 3


SOSO
SAD
HAPPY
SOSO
SOSO
SOSO
SAD
SOSO
SOSO
SOSO
SOSO
SOSO
SOSO
SAD






Example

Input

4 5
1 3 5
3 4 6
4 2 7
2 1 18
2 3 12


Output

SAD
SAD
SAD
SOSO
HAPPY

Solution (in CPP):

Solution:
#include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define ALL(x) x.begin(),x.end()
#define EPS (1e-8)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define pb push_back
#define fst first
#define snd second

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;

inline bool LT(double a,double b) { return !equals(a,b) && a < b; }
inline bool LTE(double a,double b) { return equals(a,b) || a < b; }

struct Edge {
  int s,t;
  ll c;
};

const bool debug = 0;

#define MAX 100001
int V,E;
vector<Edge> edges;
vector<vector<Edge>> sG, eG;
ll mini[2][MAX];

const string HA = "HAPPY";
const string SA = "SAD";
const string SO = "SOSO";

int par[MAX];
int find(int x) {
  if( x == par[x] ) return x;
  return par[x] = find(par[x]);
}
void unite(int x,int y) {
  x = find(x), y = find(y);
  if( x != y ) par[x] = y;
}

struct Data {
  int cur;
  ll v;
  bool operator < ( const Data &d ) const {
    if( v != d.v ) return v > d.v;
    return cur < d.cur;
  }
};

void dijkstra(int sp, vector<vector<Edge>> &G) {
  int flag = sp;
  rep(i,V) mini[flag][i] = LLONG_MAX;
  mini[flag][sp] = 0;
  priority_queue<Data> Q;
  Q.push((Data){sp,0});
  while( !Q.empty() ) {
    Data d = Q.top(); Q.pop();
    rep(i,(int)G[d.cur].size()) {
      Edge &e = G[d.cur][i];
      if( mini[flag][e.t] > d.v + e.c ) {
	mini[flag][e.t] = d.v + e.c;
	Q.push((Data){e.t, mini[flag][e.t]});
      }
    }
  }
}

typedef vector<ii>   vii;
typedef vector<int>   vi;
const int DFS_WHITE = -1; // unvisited
const int DFS_BLACK =  1; // visited



struct EdgeH { int src,dst,w; };
const int MAX_V = MAX; //
int V2,E2; //
vector<EdgeH> G[MAX_V]; // undirected graph
vector<int> dfs_num,dfs_low,dfs_parent,articulation_vertex;
int dfsNumberCounter, dfsRoot, rootChildren;
set<ii> ban;

#define UNVISITED -1
void articulationPointAndBridge(int u){
  dfs_low[u] = dfs_num[u] = dfsNumberCounter++;
  rep(j,(int)G[u].size()){
    EdgeH &v = G[u][j];
    if( dfs_num[v.dst] == UNVISITED ) {
      dfs_parent[v.dst] = u;
      if( u == dfsRoot ) rootChildren++;

      articulationPointAndBridge(v.dst);

      if( dfs_low[v.dst] >= dfs_num[u] ) articulation_vertex[u] = true;
      if( dfs_low[v.dst] >  dfs_num[u] ) {
        //printf("Edge (%d,%d) is a bridge\n",u,v.dst);
	ban.insert(ii(u,v.dst));
	ban.insert(ii(v.dst,u));
      }
      dfs_low[u] = min(dfs_low[u],dfs_low[v.dst]);
    } else if( v.dst != dfs_parent[u] ) {
      dfs_low[u] = min(dfs_low[u],dfs_num[v.dst]);
    }
  }
}

void calculateArticulationPointAndBridge(){
  dfsNumberCounter = 0;
  dfs_num.assign(V2,UNVISITED);
  dfs_low.assign(V2,0);
  dfs_parent.assign(V2,0);
  articulation_vertex.assign(V2,0);
  
  //printf("Bridges:\n");
  rep(i,V2) {
    if( dfs_num[i] == UNVISITED ) {
      dfsRoot = i, rootChildren = 0;
      articulationPointAndBridge(i);
      articulation_vertex[dfsRoot] = ( rootChildren > 1 );
    }
  }
  
  //printf("Articulation Points:\n");
  //rep(i,V2) if( articulation_vertex[i] ) printf("Vertex %d\n",i);
}




void constructMinGraph(ll default_mini, set<ii> &S) {
  rep(i,E) {
    Edge &e = edges[i];
    if( mini[0][e.s] == LLONG_MAX ) continue;
    if( mini[1][e.t] == LLONG_MAX ) continue;
    if( mini[0][e.s] + e.c + mini[1][e.t] == default_mini ) {
      if( debug ) {
	cout << "  ADD (" << e.s << ", " << e.t << ")" << endl;
      }
      S.insert(ii(e.s, e.t));
      G[e.s].pb((EdgeH){e.s, e.t, (int)e.c});
      G[e.t].pb((EdgeH){e.t, e.s, (int)e.c});
    }
  }
  V2 = V;
}

map<ii,int> mini_mp;
void solve() {
  dijkstra(0,sG);
  dijkstra(1,eG);
  sG.clear();
  eG.clear();

  if( debug ) {
    cout << "* min dist info -- from source" << endl;
    rep(i,V) {
      cout << "  - mini[0][" << i << "] = " << mini[0][i] << endl;
    }
    cout << "* min dist info -- from dst" << endl;
    rep(i,V) {
      cout << "  - mini[1][" << i << "] = " << mini[1][i] << endl;
    }    
  }
  
  ll default_mini = mini[0][1];
  if( debug ) {
    cout << "* default mini = " << default_mini << endl;
    cout << "* grahp construction for max flow" << endl;
  }
  set<ii> S;
  constructMinGraph(default_mini, S);
  calculateArticulationPointAndBridge();
  
  rep(i,E) {
    Edge &e = edges[i];
    if( find(0) != find(e.s) ) {
      puts(SO.c_str());
      continue;
    }
    if( mini[0][e.t] != LLONG_MAX && mini[1][e.s] != LLONG_MAX ) {
      ll cost = mini[0][e.t] + e.c + mini[1][e.s];
      if( cost <  default_mini ) { puts(HA.c_str()); continue; }
      if( cost == default_mini ) { puts(SO.c_str()); continue; }
    }

    if( mini_mp[ii(e.s,e.t)] != e.c ) {
      puts(SO.c_str());
      continue;
    }
    
    if( !S.count(ii(e.s,e.t)) ) {
      puts(SO.c_str());
      continue;
    }

    if( !ban.count(ii(e.s, e.t)) ) {
      puts(SO.c_str());
      continue;
    }
    
    puts(SA.c_str());
  }
}

int main() {
  scanf("%d%d",&V,&E);
  int a,b,c;
  sG.resize(V);
  eG.resize(V);
  rep(i,V) par[i] = i;
  rep(i,E) {
    scanf("%d%d%d",&a,&b,&c);
    --a, --b;
    if( mini_mp.count(ii(a,b)) ) {
      mini_mp[ii(a,b)] = min(mini_mp[ii(a,b)], c);
    }  else {
      mini_mp[ii(a,b)] = c;
    }
    edges.pb((Edge){a,b,c});
    sG[a].pb((Edge){a,b,c});
    eG[b].pb((Edge){b,a,c});
    unite(a,b);
  }
  solve();
  return 0;
}