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 <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <queue>
#define llint long long
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;

struct edge{
  llint to, cost, id;
  edge(){}
  edge(llint a, llint b, llint c){
    to = a, cost = b, id = c;
  }
};

llint n, m;
vector<edge> G[100005], revG[100005];
vector<llint> pathG[100005];
llint distS[100005], distT[100005];
bool path[100005], only[100005];
const int S = 1, T = 2;
llint ans[100005];
bool used[100005], valid[100005];
vector<llint> brgG[100005], revbrgG[100005];
llint scc[100005];

void pathdfs(int v)
{
  if(used[v]) return;
  used[v] = true;
  for(int i = 0; i < revG[v].size(); i++){
    if(!path[revG[v][i].id]) continue;
    valid[revG[v][i].id] = true;
    pathG[revG[v][i].to].push_back(v);
    pathG[v].push_back(revG[v][i].to);
    pathdfs(revG[v][i].to);
  }
}

void brgdfs(int v, int pre)
{
  if(used[v]) return;
  used[v] = true;
  for(int i = 0; i < pathG[v].size(); i++){
    if(pathG[v][i] == pre) continue;
    brgG[v].push_back(pathG[v][i]);
    revbrgG[pathG[v][i]].push_back(v);
    brgdfs(pathG[v][i], v);
  }
}

vector<llint> topo;
void tpsort(llint v, llint pre)
{
  used[v] = true;
  for(int i = 0; i < brgG[v].size(); i++){
    if(brgG[v][i] == pre) continue;
    if(used[brgG[v][i]]) continue;
    tpsort(brgG[v][i], v);
  }
  topo.push_back(v);
}

void sccdfs(int v, int pre, int id)
{
  used[v] = true;
  scc[v] = id;
  for(int i = 0; i < revbrgG[v].size(); i++){
    if(revbrgG[v][i] == pre) continue;
    if(used[revbrgG[v][i]]) continue;
    sccdfs(revbrgG[v][i], v, id);
  }
}

void findBridge(){
  for(int i = 1; i <= n; i++) used[i] = false;
  brgdfs(1, -1);

  for(int i = 1; i <= n; i++) used[i] = false;
  for(int i = 1; i <= n; i++){
    if(!used[i]) tpsort(i, -1);
  }
  reverse(topo.begin(), topo.end());

  int id = 1;
  for(int i = 1; i <= n; i++) used[i] = false;
  for(int i = 0; i < topo.size(); i++){
    if(!used[topo[i]]) sccdfs(topo[i], -1, id++);
  }

  for(int i = 1; i <= n; i++){
    for(int j = 0; j < G[i].size(); j++){
      if(!path[G[i][j].id] || !valid[G[i][j].id]) continue;
      if(scc[i] != scc[G[i][j].to]) only[G[i][j].id] = true;
    }
  }
}

void dijkstra(vector<edge> G[], llint S, llint dist[])
{
  for(int i = 1; i <= n; i++) used[i] = false;
  for(int i = 1; i <= n; i++) dist[i] = inf;
  used[S] = 0, dist[S] = 0;

  priority_queue<P, vector<P>, greater<P> > Q;
  Q.push(make_pair(0LL, S));
  while(Q.size()){
    llint d = Q.top().first;
    llint v = Q.top().second;
    Q.pop();
    if(dist[v] < d)  continue;
    for(int i = 0; i < G[v].size(); i++){
      llint u = G[v][i].to;
      if(dist[u] > dist[v] + G[v][i].cost){
        dist[u] = dist[v] + G[v][i].cost;
        Q.push(make_pair(dist[u], u));
      }
    }
  }
}

int main(void)
{
  cin >> n >> m;
  llint a, b, c;
  for(int i = 1; i <= m; i++){
    cin >> a >> b >> c;
    G[a].push_back(edge(b, c, i));
    revG[b].push_back(edge(a, c, i));
  }

  dijkstra(G, 1, distS);
  dijkstra(revG, 2, distT);

  for(int i = 1; i <= n; i++){
    for(int j = 0; j < G[i].size(); j++){
      if(distS[i] + G[i][j].cost == distS[G[i][j].to]){
        path[G[i][j].id] = true;
      }
    }
  }

  for(int i = 1; i <= n; i++) used[i] = false;
  pathdfs(T);
  findBridge();

  for(int i = 1; i <= n; i++){
    for(int j = 0; j < G[i].size(); j++){
      if(!path[G[i][j].id]){
        if(distS[T] > distS[G[i][j].to] + G[i][j].cost + distT[i]) ans[G[i][j].id] = 1;
        else ans[G[i][j].id] = 0;
      }
      else{
        if(only[G[i][j].id]) ans[G[i][j].id] = -1;
        else ans[G[i][j].id] = 0;
      }
    }
  }

  for(int i = 1;i <= m; i++){
    if(ans[i] == 1) cout << "HAPPY";
    if(ans[i] == 0) cout << "SOSO";
    if(ans[i] == -1) cout << "SAD";
    cout << endl;
  }
  return 0;
}